<!doctype html>
<html lang="en" data-color-mode="dark">
<head>
<meta charset="utf-8">
<title>Laravel 8 备忘清单
 &#x26;  laravel cheatsheet &#x26;  Quick Reference</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta description="Laravel 8Laravel 8 备忘清单是最好的 Laravel 代码段和备忘清单参考

入门，为开发人员分享快速参考备忘单。">
<meta keywords="laravel,reference,Quick,Reference,cheatsheet,cheat,sheet">
<link rel="icon" href="data:image/svg+xml,%3Csvg%20viewBox%3D%220%200%2024%2024%22%20fill%3D%22none%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20height%3D%221em%22%20width%3D%221em%22%3E%20%3Cpath%20d%3D%22m21.66%2010.44-.98%204.18c-.84%203.61-2.5%205.07-5.62%204.77-.5-.04-1.04-.13-1.62-.27l-1.68-.4c-4.17-.99-5.46-3.05-4.48-7.23l.98-4.19c.2-.85.44-1.59.74-2.2%201.17-2.42%203.16-3.07%206.5-2.28l1.67.39c4.19.98%205.47%203.05%204.49%207.23Z%22%20fill%3D%22%23c9d1d9%22%2F%3E%20%3Cpath%20d%3D%22M15.06%2019.39c-.62.42-1.4.77-2.35%201.08l-1.58.52c-3.97%201.28-6.06.21-7.35-3.76L2.5%2013.28c-1.28-3.97-.22-6.07%203.75-7.35l1.58-.52c.41-.13.8-.24%201.17-.31-.3.61-.54%201.35-.74%202.2l-.98%204.19c-.98%204.18.31%206.24%204.48%207.23l1.68.4c.58.14%201.12.23%201.62.27Zm2.43-8.88c-.06%200-.12-.01-.19-.02l-4.85-1.23a.75.75%200%200%201%20.37-1.45l4.85%201.23a.748.748%200%200%201-.18%201.47Z%22%20fill%3D%22%23228e6c%22%20%2F%3E%20%3Cpath%20d%3D%22M14.56%2013.89c-.06%200-.12-.01-.19-.02l-2.91-.74a.75.75%200%200%201%20.37-1.45l2.91.74c.4.1.64.51.54.91-.08.34-.38.56-.72.56Z%22%20fill%3D%22%23228e6c%22%20%2F%3E%20%3C%2Fsvg%3E" type="image/svg+xml">
<link rel="stylesheet" href="../style/style.css">
<link rel="stylesheet" href="../style/katex.css">
</head>
<body><nav class="header-nav"><div class="max-container"><a href="../index.html" class="logo"><svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" height="1em" width="1em">
  <path d="m21.66 10.44-.98 4.18c-.84 3.61-2.5 5.07-5.62 4.77-.5-.04-1.04-.13-1.62-.27l-1.68-.4c-4.17-.99-5.46-3.05-4.48-7.23l.98-4.19c.2-.85.44-1.59.74-2.2 1.17-2.42 3.16-3.07 6.5-2.28l1.67.39c4.19.98 5.47 3.05 4.49 7.23Z" fill="#c9d1d9"></path>
  <path d="M15.06 19.39c-.62.42-1.4.77-2.35 1.08l-1.58.52c-3.97 1.28-6.06.21-7.35-3.76L2.5 13.28c-1.28-3.97-.22-6.07 3.75-7.35l1.58-.52c.41-.13.8-.24 1.17-.31-.3.61-.54 1.35-.74 2.2l-.98 4.19c-.98 4.18.31 6.24 4.48 7.23l1.68.4c.58.14 1.12.23 1.62.27Zm2.43-8.88c-.06 0-.12-.01-.19-.02l-4.85-1.23a.75.75 0 0 1 .37-1.45l4.85 1.23a.748.748 0 0 1-.18 1.47Z" fill="#228e6c"></path>
  <path d="M14.56 13.89c-.06 0-.12-.01-.19-.02l-2.91-.74a.75.75 0 0 1 .37-1.45l2.91.74c.4.1.64.51.54.91-.08.34-.38.56-.72.56Z" fill="#228e6c"></path>
</svg>
<span class="title">Quick Reference</span></a><div class="menu"><a href="javascript:void(0);" class="searchbtn" id="searchbtn"><svg xmlns="http://www.w3.org/2000/svg" height="1em" width="1em" viewBox="0 0 18 18">
  <path fill="currentColor" d="M17.71,16.29 L14.31,12.9 C15.4069846,11.5024547 16.0022094,9.77665502 16,8 C16,3.581722 12.418278,0 8,0 C3.581722,0 0,3.581722 0,8 C0,12.418278 3.581722,16 8,16 C9.77665502,16.0022094 11.5024547,15.4069846 12.9,14.31 L16.29,17.71 C16.4777666,17.8993127 16.7333625,18.0057983 17,18.0057983 C17.2666375,18.0057983 17.5222334,17.8993127 17.71,17.71 C17.8993127,17.5222334 18.0057983,17.2666375 18.0057983,17 C18.0057983,16.7333625 17.8993127,16.4777666 17.71,16.29 Z M2,8 C2,4.6862915 4.6862915,2 8,2 C11.3137085,2 14,4.6862915 14,8 C14,11.3137085 11.3137085,14 8,14 C4.6862915,14 2,11.3137085 2,8 Z"></path>
</svg><span>搜索</span><span>⌘K</span></a><a href="https://github.com/jaywcjlove/reference/blob/main/docs/laravel.md" class="" target="__blank"><svg viewBox="0 0 36 36" fill="currentColor" height="1em" width="1em"><path d="m33 6.4-3.7-3.7a1.71 1.71 0 0 0-2.36 0L23.65 6H6a2 2 0 0 0-2 2v22a2 2 0 0 0 2 2h22a2 2 0 0 0 2-2V11.76l3-3a1.67 1.67 0 0 0 0-2.36ZM18.83 20.13l-4.19.93 1-4.15 9.55-9.57 3.23 3.23ZM29.5 9.43 26.27 6.2l1.85-1.85 3.23 3.23Z"></path><path fill="none" d="M0 0h36v36H0z"></path></svg><span>编辑</span></a><button id="darkMode" type="button"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" class="light" height="1em" width="1em">
  <path d="M6.995 12c0 2.761 2.246 5.007 5.007 5.007s5.007-2.246 5.007-5.007-2.246-5.007-5.007-5.007S6.995 9.239 6.995 12zM11 19h2v3h-2zm0-17h2v3h-2zm-9 9h3v2H2zm17 0h3v2h-3zM5.637 19.778l-1.414-1.414 2.121-2.121 1.414 1.414zM16.242 6.344l2.122-2.122 1.414 1.414-2.122 2.122zM6.344 7.759 4.223 5.637l1.415-1.414 2.12 2.122zm13.434 10.605-1.414 1.414-2.122-2.122 1.414-1.414z"></path>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 24 24" class="dark" height="1em" width="1em">
  <path d="M12 11.807A9.002 9.002 0 0 1 10.049 2a9.942 9.942 0 0 0-5.12 2.735c-3.905 3.905-3.905 10.237 0 14.142 3.906 3.906 10.237 3.905 14.143 0a9.946 9.946 0 0 0 2.735-5.119A9.003 9.003 0 0 1 12 11.807z"></path>
</svg>
</button><script src="../js/dark.js"></script><a href="https://github.com/jaywcjlove/reference" class="" target="__blank"><svg viewBox="0 0 16 16" fill="currentColor" height="1em" width="1em"><path d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.012 8.012 0 0 0 16 8c0-4.42-3.58-8-8-8z"></path></svg></a></div></div></nav><div class="wrap h1body-exist max-container"><header class="wrap-header h1wrap"><h1 id="laravel-8-备忘清单"><svg viewBox="0 0 256 264" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMidYMid" height="1em" width="1em">
  <path d="M255.856 59.62c.095.351.144.713.144 1.077v56.568c0 1.478-.79 2.843-2.073 3.578L206.45 148.18v54.18a4.135 4.135 0 0 1-2.062 3.579l-99.108 57.053c-.227.128-.474.21-.722.299-.093.03-.18.087-.278.113a4.15 4.15 0 0 1-2.114 0c-.114-.03-.217-.093-.325-.134-.227-.083-.464-.155-.68-.278L2.073 205.938A4.128 4.128 0 0 1 0 202.36V32.656c0-.372.052-.733.144-1.083.031-.119.103-.227.145-.346.077-.216.15-.438.263-.639.077-.134.19-.242.283-.366.119-.165.227-.335.366-.48.119-.118.274-.206.408-.309.15-.124.283-.258.453-.356h.005L51.613.551a4.135 4.135 0 0 1 4.125 0l49.546 28.526h.01c.165.104.305.232.454.351.134.103.284.196.402.31.145.149.248.32.371.484.088.124.207.232.279.366.118.206.185.423.268.64.041.118.113.226.144.35.095.351.144.714.145 1.078V138.65l41.286-23.773V60.692c0-.36.052-.727.145-1.072.036-.124.103-.232.144-.35.083-.217.155-.44.268-.64.077-.134.19-.242.279-.366.123-.165.226-.335.37-.48.12-.118.269-.206.403-.309.155-.124.289-.258.454-.356h.005l49.551-28.526a4.13 4.13 0 0 1 4.125 0l49.546 28.526c.175.103.309.232.464.35.128.104.278.197.397.31.144.15.247.32.37.485.094.124.207.232.28.366.118.2.185.423.267.64.047.118.114.226.145.35Zm-8.115 55.258v-47.04l-17.339 9.981-23.953 13.792v47.04l41.297-23.773h-.005Zm-49.546 85.095V152.9l-23.562 13.457-67.281 38.4v47.514l90.843-52.3ZM8.259 39.796v160.177l90.833 52.294v-47.505L51.64 177.906l-.015-.01-.02-.01c-.16-.093-.295-.227-.444-.34-.13-.104-.279-.186-.392-.3l-.01-.015c-.134-.129-.227-.289-.34-.433-.104-.14-.227-.258-.31-.402l-.005-.016c-.093-.154-.15-.34-.217-.515-.067-.155-.154-.3-.196-.464v-.005c-.051-.196-.061-.403-.082-.604-.02-.154-.062-.309-.062-.464V63.57L25.598 49.772l-17.339-9.97v-.006ZM53.681 8.893 12.399 32.656l41.272 23.762L94.947 32.65 53.671 8.893h.01Zm21.468 148.298 23.948-13.786V39.796L81.76 49.778 57.805 63.569v103.608l17.344-9.986ZM202.324 36.935l-41.276 23.762 41.276 23.763 41.271-23.768-41.27-23.757Zm-4.13 54.676-23.953-13.792-17.338-9.981v47.04l23.948 13.787 17.344 9.986v-47.04Zm-94.977 106.006 60.543-34.564 30.264-17.272-41.246-23.747-47.489 27.34-43.282 24.918 41.21 23.325Z" fill="currentColor"></path>
</svg>
<a aria-hidden="true" tabindex="-1" href="#laravel-8-备忘清单"><span class="icon icon-link"></span></a>Laravel 8 备忘清单</h1><div class="wrap-body">
<p>Laravel 8 备忘清单是最好的 Laravel 代码段和备忘清单参考</p>
</div></header><div class="menu-tocs"><div class="menu-btn"><svg aria-hidden="true" fill="currentColor" height="1em" width="1em" viewBox="0 0 16 16" version="1.1" data-view-component="true">
  <path fill-rule="evenodd" d="M2 4a1 1 0 100-2 1 1 0 000 2zm3.75-1.5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zm0 5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zm0 5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zM3 8a1 1 0 11-2 0 1 1 0 012 0zm-1 6a1 1 0 100-2 1 1 0 000 2z"></path>
</svg></div><div class="menu-modal"><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#入门">入门</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#artisan">Artisan</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#composer">Composer</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#config">Config</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#environment">Environment</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#log-日志">Log 日志</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#url">URL</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#event">Event</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#pagination">Pagination</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#lang">Lang</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#file">File</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#ssh">SSH</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#cookie">Cookie</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#unittest">UnitTest</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#cache">Cache</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#session">Session</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#response">Response</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#request">Request</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#container">Container</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#redirect">Redirect</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#security">Security</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#queue">Queue</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#view">View</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#validation">Validation</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#rules">Rules</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#form">Form</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#form-elements">Form Elements</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#string">String</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#blade">Blade</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#html">HTML</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#db">DB</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#基本数据库使用">基本数据库使用</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#查询生成器">查询生成器</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#joins">Joins</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#aggregates">Aggregates</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#原始表达式">原始表达式</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#inserts-插入">Inserts 插入</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#updates-更新">Updates 更新</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#deletes-删除">Deletes 删除</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#unions-联合">Unions 联合</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#input">Input</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#input-1">Input</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#session-input-flash">Session Input (flash)</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#files">Files</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#路由">路由</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#route">Route</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#restful-控制器">RESTful 控制器</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#触发错误">触发错误</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#路由参数">路由参数</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#http-动词">HTTP 动词</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#secure-routestbd">Secure Routes(TBD)</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#路由约束">路由约束</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#http-中间件">HTTP 中间件</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#命名路由">命名路由</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#路由前缀">路由前缀</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#路由命名空间">路由命名空间</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#子域路由">子域路由</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#model">Model</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#基本用法">基本用法</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#more">More</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#soft-delete">Soft Delete</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#events">Events</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#eloquent-configuration">Eloquent Configuration</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#schema">Schema</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#schema-1">Schema</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#indexes">Indexes</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#foreign-keys">Foreign Keys</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#列类型---增量">列类型 - 增量</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#列类型---字符串和文本">列类型 - 字符串和文本</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#列类型---数字">列类型 - 数字</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#列类型---日期和时间">列类型 - 日期和时间</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#列类型---添加-created_at-和-updated_at-列">列类型 - 添加 created_at 和 updated_at 列</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#列类型---其他">列类型 - 其他</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#mail">Mail</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#mail-1">Mail</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#messages">Messages</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#auth">Auth</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#authentication">Authentication</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#authorization">Authorization</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#helper">Helper</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#arrays">Arrays</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#paths">Paths</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#miscellaneous">Miscellaneous</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#strings">Strings</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#urls-and-links">URLs and Links</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#另见">另见</a></div></div><div class="h1wrap-body"><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="入门"><a aria-hidden="true" tabindex="-1" href="#入门"><span class="icon icon-link"></span></a>入门</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist row-span-10"><div class="wrap-header h3wrap"><h3 id="artisan"><a aria-hidden="true" tabindex="-1" href="#artisan"><span class="icon icon-link"></span></a>Artisan</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-10-->
<pre class="wrap-text "><code class="language-bash code-highlight"><span class="code-line"><span class="token comment"># 5.1.11 新增：http://laravel.com/docs/authorization#creating-policies</span>
</span><span class="code-line">$ php artisan make:policy PostPolicy
</span><span class="code-line"><span class="token comment"># 显示给定命令的帮助</span>
</span><span class="code-line">$ php artisan <span class="token parameter variable">--help</span> 或 <span class="token parameter variable">-h</span>
</span><span class="code-line"><span class="token comment"># 不输出任何信息</span>
</span><span class="code-line">$ php artisan <span class="token parameter variable">--quiet</span> 或 <span class="token parameter variable">-q</span>
</span><span class="code-line"><span class="token comment"># 显示此应用程序版本</span>
</span><span class="code-line">$ php artisan <span class="token parameter variable">--version</span> 或 <span class="token parameter variable">-V</span>
</span><span class="code-line"><span class="token comment"># 不要问任何互动问题</span>
</span><span class="code-line">$ php artisan --no-interaction OR <span class="token parameter variable">-n</span>
</span><span class="code-line"><span class="token comment"># 强制 ANSI 输出</span>
</span><span class="code-line">$ php artisan <span class="token parameter variable">--ansi</span>
</span><span class="code-line"><span class="token comment"># 禁用 ANSI 输出</span>
</span><span class="code-line">$ php artisan --no-ansi
</span><span class="code-line"><span class="token comment"># 命令应运行的环境</span>
</span><span class="code-line">$ php artisan <span class="token parameter variable">--env</span>
</span><span class="code-line"><span class="token comment"># -v|vv|vvv 增加消息的详细程度：1 表示正常输出，2 表示更详细的输出，3 表示调试</span>
</span><span class="code-line">$ php artisan <span class="token parameter variable">--verbose</span>
</span><span class="code-line"><span class="token comment"># 删除编译的类文件</span>
</span><span class="code-line">$ php artisan clear-compiled
</span><span class="code-line"><span class="token comment"># 显示当前框架环境</span>
</span><span class="code-line">$ php artisan <span class="token function">env</span>
</span><span class="code-line"><span class="token comment"># 显示命令的帮助</span>
</span><span class="code-line">$ php artisan <span class="token builtin class-name">help</span>
</span><span class="code-line"><span class="token comment"># 列出命令</span>
</span><span class="code-line">$ php artisan list
</span><span class="code-line"><span class="token comment"># 与您的应用程序交互</span>
</span><span class="code-line">$ php artisan tinker
</span><span class="code-line"><span class="token comment"># 将应用程序置于维护模式</span>
</span><span class="code-line">$ php artisan down
</span><span class="code-line"><span class="token comment"># 使应用程序退出维护模式</span>
</span><span class="code-line">$ php artisan up
</span><span class="code-line"><span class="token comment"># 优化框架以获得更好的性能</span>
</span><span class="code-line"><span class="token comment"># --force 强制写入已编译的类文件</span>
</span><span class="code-line"><span class="token comment"># --psr 不要优化 Composer dump-autoload</span>
</span><span class="code-line">$ php artisan optimize <span class="token punctuation">[</span>--force<span class="token punctuation">]</span> <span class="token punctuation">[</span>--psr<span class="token punctuation">]</span>
</span><span class="code-line"><span class="token comment"># 在 PHP 开发服务器上提供应用程序</span>
</span><span class="code-line">$ php artisan serve
</span><span class="code-line"><span class="token comment"># 更改默认端口</span>
</span><span class="code-line">$ php artisan serve <span class="token parameter variable">--port</span> <span class="token number">8080</span>
</span><span class="code-line"><span class="token comment"># 让它在本地主机之外工作</span>
</span><span class="code-line">$ php artisan serve <span class="token parameter variable">--host</span> <span class="token number">0.0</span>.0.0
</span><span class="code-line"><span class="token comment"># 设置应用命名空间</span>
</span><span class="code-line">$ php artisan app:name namespace
</span><span class="code-line"><span class="token comment"># 刷新过期的密码重置令牌</span>
</span><span class="code-line">$ php artisan auth:clear-resets
</span><span class="code-line"><span class="token comment"># 刷新应用程序缓存</span>
</span><span class="code-line">$ php artisan cache:clear
</span><span class="code-line"><span class="token comment"># 为缓存数据库表创建迁移</span>
</span><span class="code-line">$ php artisan cache:table
</span><span class="code-line"><span class="token comment"># 创建缓存文件以加快配置加载</span>
</span><span class="code-line">$ php artisan config:cache
</span><span class="code-line"><span class="token comment"># 删除配置缓存文件</span>
</span><span class="code-line">$ php artisan config:clear
</span><span class="code-line"><span class="token comment"># 在节目中</span>
</span><span class="code-line"><span class="token variable">$exitCode</span> <span class="token operator">=</span> Artisan::call<span class="token punctuation">(</span><span class="token string">'config:cache'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment"># 用记录播种数据库</span>
</span><span class="code-line"><span class="token comment"># --class 根播种器的类名（默认值："DatabaseSeeder"）</span>
</span><span class="code-line"><span class="token comment"># --database 种子的数据库连接</span>
</span><span class="code-line"><span class="token comment"># --force 强制操作在生产中运行</span>
</span><span class="code-line">$ php artisan db:seed <span class="token punctuation">[</span>--class<span class="token punctuation">[</span><span class="token operator">=</span><span class="token string">"..."</span><span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token punctuation">[</span>--database<span class="token punctuation">[</span><span class="token operator">=</span><span class="token string">"..."</span><span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token punctuation">[</span>--force<span class="token punctuation">]</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 根据注册生成缺失的事件和处理程序</span>
</span><span class="code-line">$ php artisan event:generate
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 创建一个新的命令处理程序类</span>
</span><span class="code-line"><span class="token comment"># --command 处理程序处理的命令类</span>
</span><span class="code-line">$ php artisan handler:command <span class="token punctuation">[</span>--command<span class="token operator">=</span><span class="token string">"..."</span><span class="token punctuation">]</span> name
</span><span class="code-line"><span class="token comment"># 创建一个新的事件处理程序类</span>
</span><span class="code-line"><span class="token comment"># --event 处理程序处理的事件类</span>
</span><span class="code-line"><span class="token comment"># --queued 表示事件处理程序应该排队</span>
</span><span class="code-line">$ php artisan handler:event <span class="token punctuation">[</span>--event<span class="token operator">=</span><span class="token string">"..."</span><span class="token punctuation">]</span> <span class="token punctuation">[</span>--queued<span class="token punctuation">]</span> name
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 设置应用程序密钥</span>
</span><span class="code-line">$ php artisan key:generate
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 默认情况下，这会创建一个不会推送到队列的自我处理命令</span>
</span><span class="code-line"><span class="token comment"># 传递 --handler 标志以生成处理程序，传递 --queued 标志以使其排队</span>
</span><span class="code-line">$ php artisan make:command <span class="token punctuation">[</span>--handler<span class="token punctuation">]</span> <span class="token punctuation">[</span>--queued<span class="token punctuation">]</span> name
</span><span class="code-line"><span class="token comment"># 创建一个新的 Artisan 命令</span>
</span><span class="code-line"><span class="token comment">#   --command 应分配的终端命令。 （默认值：“命令：名称”）</span>
</span><span class="code-line">make:console <span class="token punctuation">[</span>--command<span class="token punctuation">[</span><span class="token operator">=</span><span class="token string">"..."</span><span class="token punctuation">]</span><span class="token punctuation">]</span> name
</span><span class="code-line"><span class="token comment"># 创建一个新的足智多谋的控制器</span>
</span><span class="code-line"><span class="token comment"># --plain 生成一个空的控制器类</span>
</span><span class="code-line">$ php artisan make:controller <span class="token punctuation">[</span>--plain<span class="token punctuation">]</span> name
</span><span class="code-line">$ php artisan make:controller App<span class="token punctuation">\</span><span class="token punctuation">\</span>Admin<span class="token punctuation">\</span><span class="token punctuation">\</span>Http<span class="token punctuation">\</span><span class="token punctuation">\</span>Controllers<span class="token punctuation">\</span><span class="token punctuation">\</span>DashboardController
</span><span class="code-line"><span class="token comment"># 创建一个新的事件类</span>
</span><span class="code-line">$ php artisan make:event name
</span><span class="code-line"><span class="token comment"># 新建一个中间件类</span>
</span><span class="code-line">$ php artisan make:middleware name
</span><span class="code-line"><span class="token comment"># 创建一个新的迁移文件</span>
</span><span class="code-line"><span class="token comment"># --create 要创建的表</span>
</span><span class="code-line"><span class="token comment"># --table 要迁移的表</span>
</span><span class="code-line">$ php artisan make:migration <span class="token punctuation">[</span>--create<span class="token punctuation">[</span><span class="token operator">=</span><span class="token string">"..."</span><span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token punctuation">[</span>--table<span class="token punctuation">[</span><span class="token operator">=</span><span class="token string">"..."</span><span class="token punctuation">]</span><span class="token punctuation">]</span> name
</span><span class="code-line"><span class="token comment"># 创建一个新的 Eloquent 模型类</span>
</span><span class="code-line">$ php artisan make:model name
</span><span class="code-line"><span class="token comment"># 创建一个新的服务提供者类</span>
</span><span class="code-line">$ php artisan make:provider name
</span><span class="code-line"><span class="token comment"># 新建一个表单请求类</span>
</span><span class="code-line">$ php artisan make:request name
</span><span class="code-line"><span class="token comment"># 数据库迁移</span>
</span><span class="code-line"><span class="token comment"># --database 要使用的数据库连接</span>
</span><span class="code-line"><span class="token comment"># --force 强制操作在生产中运行</span>
</span><span class="code-line"><span class="token comment"># --path 要执行的迁移文件的路径</span>
</span><span class="code-line"><span class="token comment"># --pretend 转储将要运行的 SQL 查询</span>
</span><span class="code-line"><span class="token comment"># --seed 指示是否应重新运行种子任务</span>
</span><span class="code-line">$ php artisan migrate <span class="token punctuation">[</span>--database<span class="token punctuation">[</span><span class="token operator">=</span><span class="token string">"..."</span><span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token punctuation">[</span>--force<span class="token punctuation">]</span> <span class="token punctuation">[</span>--path<span class="token punctuation">[</span><span class="token operator">=</span><span class="token string">"..."</span><span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token punctuation">[</span>--pretend<span class="token punctuation">]</span> <span class="token punctuation">[</span>--seed<span class="token punctuation">]</span>
</span><span class="code-line"><span class="token comment"># 创建迁移存储库</span>
</span><span class="code-line">$ php artisan migrate:install <span class="token punctuation">[</span>--database<span class="token punctuation">[</span><span class="token operator">=</span><span class="token string">"..."</span><span class="token punctuation">]</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token comment"># 创建一个新的迁移文件</span>
</span><span class="code-line"><span class="token comment"># --seeder 根播种机的类名。</span>
</span><span class="code-line">$ php artisan migrate:refresh <span class="token punctuation">[</span>--database<span class="token punctuation">[</span><span class="token operator">=</span><span class="token string">"..."</span><span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token punctuation">[</span>--force<span class="token punctuation">]</span> <span class="token punctuation">[</span>--seed<span class="token punctuation">]</span> <span class="token punctuation">[</span>--seeder<span class="token punctuation">[</span><span class="token operator">=</span><span class="token string">"..."</span><span class="token punctuation">]</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token comment"># 回滚所有数据库迁移</span>
</span><span class="code-line"><span class="token comment"># --pretend 转储将要运行的 SQL 查询。</span>
</span><span class="code-line">$ php artisan migrate:reset <span class="token punctuation">[</span>--database<span class="token punctuation">[</span><span class="token operator">=</span><span class="token string">"..."</span><span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token punctuation">[</span>--force<span class="token punctuation">]</span> <span class="token punctuation">[</span>--pretend<span class="token punctuation">]</span>
</span><span class="code-line"><span class="token comment"># 回滚上次数据库迁移</span>
</span><span class="code-line">$ php artisan migrate:rollback <span class="token punctuation">[</span>--database<span class="token punctuation">[</span><span class="token operator">=</span><span class="token string">"..."</span><span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token punctuation">[</span>--force<span class="token punctuation">]</span> <span class="token punctuation">[</span>--pretend<span class="token punctuation">]</span>
</span><span class="code-line"><span class="token comment"># 显示上/下迁移列表</span>
</span><span class="code-line">$ php artisan migrate:status
</span><span class="code-line"><span class="token comment"># 为队列作业数据库表创建迁移</span>
</span><span class="code-line">$ php artisan queue:table
</span><span class="code-line"><span class="token comment"># 收听给定的队列</span>
</span><span class="code-line"><span class="token comment"># --queue 要监听的队列</span>
</span><span class="code-line"><span class="token comment"># --delay 延迟失败作业的时间量（默认值：0）</span>
</span><span class="code-line"><span class="token comment"># --memory 以兆字节为单位的内存限制（默认值：128）</span>
</span><span class="code-line"><span class="token comment"># --timeout 超时前作业可能运行的秒数（默认值：60）</span>
</span><span class="code-line"><span class="token comment"># --sleep 在检查作业队列之前等待的秒数（默认值：3）</span>
</span><span class="code-line"><span class="token comment"># --tries 在记录失败之前尝试作业的次数（默认值：0）</span>
</span><span class="code-line">$ php artisan queue:listen <span class="token punctuation">[</span>--queue<span class="token punctuation">[</span><span class="token operator">=</span><span class="token string">"..."</span><span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token punctuation">[</span>--delay<span class="token punctuation">[</span><span class="token operator">=</span><span class="token string">"..."</span><span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token punctuation">[</span>--memory<span class="token punctuation">[</span><span class="token operator">=</span><span class="token string">"..."</span><span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token punctuation">[</span>--timeout<span class="token punctuation">[</span><span class="token operator">=</span><span class="token string">"..."</span><span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token punctuation">[</span>--sleep<span class="token punctuation">[</span><span class="token operator">=</span><span class="token string">"..."</span><span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token punctuation">[</span>--tries<span class="token punctuation">[</span><span class="token operator">=</span><span class="token string">"..."</span><span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token punctuation">[</span>connection<span class="token punctuation">]</span>
</span><span class="code-line"><span class="token comment"># 列出所有失败的队列作业</span>
</span><span class="code-line">$ php artisan queue:failed
</span><span class="code-line"><span class="token comment"># 为失败队列作业数据库表创建迁移</span>
</span><span class="code-line">$ php artisan queue:failed-table
</span><span class="code-line"><span class="token comment"># 刷新所有失败的队列作业</span>
</span><span class="code-line">$ php artisan queue:flush
</span><span class="code-line"><span class="token comment"># 删除失败的队列作业</span>
</span><span class="code-line">$ php artisan queue:forget
</span><span class="code-line"><span class="token comment"># 在完成当前作业后重新启动队列工作守护进程</span>
</span><span class="code-line">$ php artisan queue:restart
</span><span class="code-line"><span class="token comment"># 重试一个失败的队列作业（id：失败作业的ID）</span>
</span><span class="code-line">$ php artisan queue:retry <span class="token function">id</span>
</span><span class="code-line"><span class="token comment"># 订阅 Iron.io 推送队列的 URL</span>
</span><span class="code-line"><span class="token comment"># 队列：Iron.io 队列的名称</span>
</span><span class="code-line"><span class="token comment"># url：要订阅的URL</span>
</span><span class="code-line"><span class="token comment"># --type 队列的推送类型</span>
</span><span class="code-line">$ php artisan queue:subscribe <span class="token punctuation">[</span>--type<span class="token punctuation">[</span><span class="token operator">=</span><span class="token string">"..."</span><span class="token punctuation">]</span><span class="token punctuation">]</span> queue url
</span><span class="code-line"><span class="token comment"># 处理队列中的下一个作业</span>
</span><span class="code-line"><span class="token comment"># --queue 要监听的队列</span>
</span><span class="code-line"><span class="token comment"># --daemon 以守护进程模式运行worker</span>
</span><span class="code-line"><span class="token comment"># --delay 延迟失败作业的时间量（默认值：0）</span>
</span><span class="code-line"><span class="token comment"># --force 即使在维护模式下也强制 worker 运行</span>
</span><span class="code-line"><span class="token comment"># --memory 以兆字节为单位的内存限制（默认值：128）</span>
</span><span class="code-line"><span class="token comment"># --sleep 没有工作时休眠的秒数（默认值：3）</span>
</span><span class="code-line"><span class="token comment"># --tries 在记录失败之前尝试作业的次数（默认值：0）</span>
</span><span class="code-line">$ php artisan queue:work <span class="token punctuation">[</span>--queue<span class="token punctuation">[</span><span class="token operator">=</span><span class="token string">"..."</span><span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token punctuation">[</span>--daemon<span class="token punctuation">]</span> <span class="token punctuation">[</span>--delay<span class="token punctuation">[</span><span class="token operator">=</span><span class="token string">"..."</span><span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token punctuation">[</span>--force<span class="token punctuation">]</span> <span class="token punctuation">[</span>--memory<span class="token punctuation">[</span><span class="token operator">=</span><span class="token string">"..."</span><span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token punctuation">[</span>--sleep<span class="token punctuation">[</span><span class="token operator">=</span><span class="token string">"..."</span><span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token punctuation">[</span>--tries<span class="token punctuation">[</span><span class="token operator">=</span><span class="token string">"..."</span><span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token punctuation">[</span>connection<span class="token punctuation">]</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 创建路由缓存文件以加快路由注册</span>
</span><span class="code-line">$ php artisan route:cache
</span><span class="code-line"><span class="token comment"># 移除路由缓存文件</span>
</span><span class="code-line">$ php artisan route:clear
</span><span class="code-line"><span class="token comment"># 列出所有注册的路由</span>
</span><span class="code-line">$ php artisan route:list
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 运行预定的命令</span>
</span><span class="code-line">$ php artisan schedule:run
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 为会话数据库表创建迁移</span>
</span><span class="code-line">$ php artisan session:table
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 从供应商包中发布任何可发布的资产</span>
</span><span class="code-line"><span class="token comment"># --force 覆盖任何现有文件</span>
</span><span class="code-line"><span class="token comment"># --provider 具有您要发布的资产的服务提供商</span>
</span><span class="code-line"><span class="token comment"># --tag 包含您要发布的资产的标签</span>
</span><span class="code-line">$ php artisan vendor:publish <span class="token punctuation">[</span>--force<span class="token punctuation">]</span> <span class="token punctuation">[</span>--provider<span class="token punctuation">[</span><span class="token operator">=</span><span class="token string">"..."</span><span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token punctuation">[</span>--tag<span class="token punctuation">[</span><span class="token operator">=</span><span class="token string">"..."</span><span class="token punctuation">]</span><span class="token punctuation">]</span>
</span><span class="code-line">$ php artisan <span class="token function">tail</span> <span class="token punctuation">[</span>--path<span class="token punctuation">[</span><span class="token operator">=</span><span class="token string">"..."</span><span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token punctuation">[</span>--lines<span class="token punctuation">[</span><span class="token operator">=</span><span class="token string">"..."</span><span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token punctuation">[</span>connection<span class="token punctuation">]</span>
</span></code></pre>
<!--rehype:className=wrap-text -->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="composer"><a aria-hidden="true" tabindex="-1" href="#composer"><span class="icon icon-link"></span></a>Composer</h3><div class="wrap-body">
<pre class="wrap-text"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token function">composer</span> create-project laravel/laravel folder_name
</span><span class="code-line">$ <span class="token function">composer</span> <span class="token function">install</span>
</span><span class="code-line">$ <span class="token function">composer</span> update
</span><span class="code-line">$ <span class="token function">composer</span> dump-autoload <span class="token punctuation">[</span>--optimize<span class="token punctuation">]</span>
</span><span class="code-line">$ <span class="token function">composer</span> self-update
</span><span class="code-line">$ <span class="token function">composer</span> require <span class="token punctuation">[</span>options<span class="token punctuation">]</span> <span class="token punctuation">[</span>--<span class="token punctuation">]</span> <span class="token punctuation">[</span>vender/packages<span class="token punctuation">]</span><span class="token punctuation">..</span>.
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="config"><a aria-hidden="true" tabindex="-1" href="#config"><span class="icon icon-link"></span></a>Config</h3><div class="wrap-body">
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token scope">Config<span class="token punctuation">::</span></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app.timezone'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 使用默认值获取</span>
</span><span class="code-line"><span class="token scope">Config<span class="token punctuation">::</span></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app.timezone'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'UTC'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 设置配置</span>
</span><span class="code-line"><span class="token scope">Config<span class="token punctuation">::</span></span><span class="token function">set</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'database.default'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'sqlite'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="environment"><a aria-hidden="true" tabindex="-1" href="#environment"><span class="icon icon-link"></span></a>Environment</h3><div class="wrap-body">
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token variable">$environment</span> <span class="token operator">=</span> <span class="token function">app</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">environment</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$environment</span> <span class="token operator">=</span> <span class="token scope">App<span class="token punctuation">::</span></span><span class="token function">environment</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$environment</span> <span class="token operator">=</span> <span class="token variable">$app</span><span class="token operator">-></span><span class="token function">environment</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 环境是当地的</span>
</span><span class="code-line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token function">environment</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'local'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment">// 环境要么是本地的，要么是暂存的……</span>
</span><span class="code-line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token function">environment</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'local'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'staging'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="log-日志"><a aria-hidden="true" tabindex="-1" href="#log-日志"><span class="icon icon-link"></span></a>Log 日志</h3><div class="wrap-body">
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token comment">// 记录器提供 RFC 5424 中定义的七个日志记录级别：</span>
</span><span class="code-line"><span class="token comment">// 调试、信息、通知、警告、错误、严重和警报</span>
</span><span class="code-line"><span class="token comment">// debug, info, notice, warning, error, critical, 和 alert</span>
</span><span class="code-line"><span class="token scope">Log<span class="token punctuation">::</span></span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'info'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Log<span class="token punctuation">::</span></span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'info'</span><span class="token punctuation">,</span><span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'context'</span><span class="token operator">=></span><span class="token string single-quoted-string">'additional info'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Log<span class="token punctuation">::</span></span><span class="token function">error</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'error'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Log<span class="token punctuation">::</span></span><span class="token function">warning</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'warning'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 获取独白实例</span>
</span><span class="code-line"><span class="token scope">Log<span class="token punctuation">::</span></span><span class="token function">getMonolog</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 添加监听器</span>
</span><span class="code-line"><span class="token scope">Log<span class="token punctuation">::</span></span><span class="token function">listen</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token variable">$level</span><span class="token punctuation">,</span> <span class="token variable">$message</span><span class="token punctuation">,</span> <span class="token variable">$context</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
<p>查询记录</p>
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token comment">// 启用日志</span>
</span><span class="code-line"><span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">connection</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">enableQueryLog</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 获取已执行查询的数组</span>
</span><span class="code-line"><span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">getQueryLog</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="url"><a aria-hidden="true" tabindex="-1" href="#url"><span class="icon icon-link"></span></a>URL</h3><div class="wrap-body">
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token scope">URL<span class="token punctuation">::</span></span><span class="token function">full</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">URL<span class="token punctuation">::</span></span><span class="token function">current</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">URL<span class="token punctuation">::</span></span><span class="token function">previous</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">URL<span class="token punctuation">::</span></span><span class="token function">to</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo/bar'</span><span class="token punctuation">,</span> <span class="token variable">$parameters</span><span class="token punctuation">,</span> <span class="token variable">$secure</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">URL<span class="token punctuation">::</span></span><span class="token function">action</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'NewsController@item'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span><span class="token operator">=></span><span class="token number">123</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 需要在适当的命名空间中</span>
</span><span class="code-line"><span class="token scope">URL<span class="token punctuation">::</span></span><span class="token function">action</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Auth\AuthController@logout'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">URL<span class="token punctuation">::</span></span><span class="token function">action</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'FooController@method'</span><span class="token punctuation">,</span> <span class="token variable">$parameters</span><span class="token punctuation">,</span> <span class="token variable">$absolute</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">URL<span class="token punctuation">::</span></span><span class="token function">route</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">,</span> <span class="token variable">$parameters</span><span class="token punctuation">,</span> <span class="token variable">$absolute</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">URL<span class="token punctuation">::</span></span><span class="token function">secure</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo/bar'</span><span class="token punctuation">,</span> <span class="token variable">$parameters</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">URL<span class="token punctuation">::</span></span><span class="token function">asset</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'css/foo.css'</span><span class="token punctuation">,</span> <span class="token variable">$secure</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">URL<span class="token punctuation">::</span></span><span class="token function">secureAsset</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'css/foo.css'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">URL<span class="token punctuation">::</span></span><span class="token function">isValidUrl</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'http://example.com'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">URL<span class="token punctuation">::</span></span><span class="token function">getRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">URL<span class="token punctuation">::</span></span><span class="token function">setRequest</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="event"><a aria-hidden="true" tabindex="-1" href="#event"><span class="icon icon-link"></span></a>Event</h3><div class="wrap-body">
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token scope">Event<span class="token punctuation">::</span></span><span class="token function">fire</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo.bar'</span><span class="token punctuation">,</span> <span class="token keyword">array</span><span class="token punctuation">(</span><span class="token variable">$bar</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 向调度程序注册一个事件侦听器</span>
</span><span class="code-line"><span class="token comment">// void listen(string|array $events, mixed $listener, int $priority)</span>
</span><span class="code-line"><span class="token scope">Event<span class="token punctuation">::</span></span><span class="token function">listen</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'App\Events\UserSignup'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token variable">$bar</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Event<span class="token punctuation">::</span></span><span class="token function">listen</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo.*'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token variable">$bar</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Event<span class="token punctuation">::</span></span><span class="token function">listen</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo.bar'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'FooHandler'</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Event<span class="token punctuation">::</span></span><span class="token function">listen</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo.bar'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'BarHandler'</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 停止传播事件</span>
</span><span class="code-line"><span class="token comment">// 您可以通过从处理程序返回 false 来执行此操作</span>
</span><span class="code-line"><span class="token scope">Event<span class="token punctuation">::</span></span><span class="token function">listen</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foor.bar'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token variable">$event</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token constant boolean">false</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Event<span class="token punctuation">::</span></span><span class="token function">subscribe</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'UserEventHandler'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="pagination"><a aria-hidden="true" tabindex="-1" href="#pagination"><span class="icon icon-link"></span></a>Pagination</h3><div class="wrap-body">
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token comment">// 自动魔术分页</span>
</span><span class="code-line"><span class="token scope">Model<span class="token punctuation">::</span></span><span class="token function">paginate</span><span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Model<span class="token punctuation">::</span></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'cars'</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">paginate</span><span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 仅“下一个”和“上一个”</span>
</span><span class="code-line"><span class="token scope">Model<span class="token punctuation">::</span></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'cars'</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">simplePaginate</span><span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 手动分页器</span>
</span><span class="code-line"><span class="token scope">Paginator<span class="token punctuation">::</span></span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$items</span><span class="token punctuation">,</span> <span class="token variable">$totalItems</span><span class="token punctuation">,</span> <span class="token variable">$perPage</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 在视图中打印页面导航器</span>
</span><span class="code-line"><span class="token variable">$variable</span><span class="token operator">-></span><span class="token function">links</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="lang"><a aria-hidden="true" tabindex="-1" href="#lang"><span class="icon icon-link"></span></a>Lang</h3><div class="wrap-body">
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token scope">App<span class="token punctuation">::</span></span><span class="token function">setLocale</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'en'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Lang<span class="token punctuation">::</span></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'messages.welcome'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Lang<span class="token punctuation">::</span></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'messages.welcome'</span><span class="token punctuation">,</span> <span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Bar'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Lang<span class="token punctuation">::</span></span><span class="token function">has</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'messages.welcome'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Lang<span class="token punctuation">::</span></span><span class="token function">choice</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'messages.apples'</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// Lang::get alias</span>
</span><span class="code-line"><span class="token function">trans</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'messages.welcome'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="file"><a aria-hidden="true" tabindex="-1" href="#file"><span class="icon icon-link"></span></a>File</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token scope">File<span class="token punctuation">::</span></span><span class="token function">exists</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'path'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">File<span class="token punctuation">::</span></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'path'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">File<span class="token punctuation">::</span></span><span class="token function">getRemote</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'path'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 通过请求获取文件的内容</span>
</span><span class="code-line"><span class="token scope">File<span class="token punctuation">::</span></span><span class="token function">getRequire</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'path'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 需要一次给定的文件</span>
</span><span class="code-line"><span class="token scope">File<span class="token punctuation">::</span></span><span class="token function">requireOnce</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'path'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 写一个文件的内容</span>
</span><span class="code-line"><span class="token scope">File<span class="token punctuation">::</span></span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'path'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'contents'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 附加到文件</span>
</span><span class="code-line"><span class="token scope">File<span class="token punctuation">::</span></span><span class="token function">append</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'path'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'data'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 删除给定路径的文件</span>
</span><span class="code-line"><span class="token scope">File<span class="token punctuation">::</span></span><span class="token function">delete</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'path'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 将文件移动到新位置</span>
</span><span class="code-line"><span class="token scope">File<span class="token punctuation">::</span></span><span class="token function">move</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'path'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'target'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 将文件复制到新位置</span>
</span><span class="code-line"><span class="token scope">File<span class="token punctuation">::</span></span><span class="token function">copy</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'path'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'target'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 从文件路径中提取文件扩展名</span>
</span><span class="code-line"><span class="token scope">File<span class="token punctuation">::</span></span><span class="token function">extension</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'path'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 获取给定文件的文件类型</span>
</span><span class="code-line"><span class="token scope">File<span class="token punctuation">::</span></span><span class="token function">type</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'path'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 获取给定文件的文件大小</span>
</span><span class="code-line"><span class="token scope">File<span class="token punctuation">::</span></span><span class="token function">size</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'path'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 获取文件的最后修改时间</span>
</span><span class="code-line"><span class="token scope">File<span class="token punctuation">::</span></span><span class="token function">lastModified</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'path'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 确定给定路径是否为目录</span>
</span><span class="code-line"><span class="token scope">File<span class="token punctuation">::</span></span><span class="token function">isDirectory</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'directory'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 确定给定路径是否可写</span>
</span><span class="code-line"><span class="token scope">File<span class="token punctuation">::</span></span><span class="token function">isWritable</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'path'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 确定给定路径是否为文件</span>
</span><span class="code-line"><span class="token scope">File<span class="token punctuation">::</span></span><span class="token function">isFile</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'file'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 查找与给定模式匹配的路径名</span>
</span><span class="code-line"><span class="token scope">File<span class="token punctuation">::</span></span><span class="token function">glob</span><span class="token punctuation">(</span><span class="token variable">$patterns</span><span class="token punctuation">,</span> <span class="token variable">$flag</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 获取目录中所有文件的数组</span>
</span><span class="code-line"><span class="token scope">File<span class="token punctuation">::</span></span><span class="token function">files</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'directory'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 从给定目录中获取所有文件（递归）</span>
</span><span class="code-line"><span class="token scope">File<span class="token punctuation">::</span></span><span class="token function">allFiles</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'directory'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 获取给定目录中的所有目录</span>
</span><span class="code-line"><span class="token scope">File<span class="token punctuation">::</span></span><span class="token function">directories</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'directory'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 创建目录</span>
</span><span class="code-line"><span class="token scope">File<span class="token punctuation">::</span></span><span class="token function">makeDirectory</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'path'</span><span class="token punctuation">,</span>  <span class="token variable">$mode</span> <span class="token operator">=</span> <span class="token number">0777</span><span class="token punctuation">,</span> <span class="token variable">$recursive</span> <span class="token operator">=</span> <span class="token constant boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 将目录从一个位置复制到另一个位置</span>
</span><span class="code-line"><span class="token scope">File<span class="token punctuation">::</span></span><span class="token function">copyDirectory</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'directory'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'destination'</span><span class="token punctuation">,</span> <span class="token variable">$options</span> <span class="token operator">=</span> <span class="token constant">null</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 递归删除目录</span>
</span><span class="code-line"><span class="token scope">File<span class="token punctuation">::</span></span><span class="token function">deleteDirectory</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'directory'</span><span class="token punctuation">,</span> <span class="token variable">$preserve</span> <span class="token operator">=</span> <span class="token constant boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 清空指定目录下的所有文件和文件夹</span>
</span><span class="code-line"><span class="token scope">File<span class="token punctuation">::</span></span><span class="token function">cleanDirectory</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'directory'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="ssh"><a aria-hidden="true" tabindex="-1" href="#ssh"><span class="icon icon-link"></span></a>SSH</h3><div class="wrap-body">
<p>执行命令</p>
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token scope">SSH<span class="token punctuation">::</span></span><span class="token function">run</span><span class="token punctuation">(</span><span class="token keyword type-hint">array</span> <span class="token variable">$commands</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">SSH<span class="token punctuation">::</span></span><span class="token function">into</span><span class="token punctuation">(</span><span class="token variable">$remote</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">run</span><span class="token punctuation">(</span><span class="token keyword type-hint">array</span> <span class="token variable">$commands</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
</span><span class="code-line"><span class="token comment">// 指定远程，否则假定默认</span>
</span><span class="code-line"><span class="token scope">SSH<span class="token punctuation">::</span></span><span class="token function">run</span><span class="token punctuation">(</span><span class="token keyword type-hint">array</span> <span class="token variable">$commands</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token variable">$line</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">echo</span> <span class="token variable">$line</span><span class="token operator">.</span><span class="token constant">PHP_EOL</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
<p>任务</p>
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token comment">// 定义</span>
</span><span class="code-line"><span class="token scope">SSH<span class="token punctuation">::</span></span><span class="token function">define</span><span class="token punctuation">(</span><span class="token variable">$taskName</span><span class="token punctuation">,</span> <span class="token keyword type-hint">array</span> <span class="token variable">$commands</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 执行</span>
</span><span class="code-line"><span class="token scope">SSH<span class="token punctuation">::</span></span><span class="token function">task</span><span class="token punctuation">(</span><span class="token variable">$taskName</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token variable">$line</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">echo</span> <span class="token variable">$line</span><span class="token operator">.</span><span class="token constant">PHP_EOL</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
<p>SFTP 上传</p>
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token scope">SSH<span class="token punctuation">::</span></span><span class="token function">put</span><span class="token punctuation">(</span><span class="token variable">$localFile</span><span class="token punctuation">,</span> <span class="token variable">$remotePath</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">SSH<span class="token punctuation">::</span></span><span class="token function">putString</span><span class="token punctuation">(</span><span class="token variable">$string</span><span class="token punctuation">,</span> <span class="token variable">$remotePath</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="cookie"><a aria-hidden="true" tabindex="-1" href="#cookie"><span class="icon icon-link"></span></a>Cookie</h3><div class="wrap-body">
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token scope">Cookie<span class="token punctuation">::</span></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Cookie<span class="token punctuation">::</span></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'default'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 创建一个永远持续的 cookie</span>
</span><span class="code-line"><span class="token scope">Cookie<span class="token punctuation">::</span></span><span class="token function">forever</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 创建一个持续 N 分钟的 cookie</span>
</span><span class="code-line"><span class="token scope">Cookie<span class="token punctuation">::</span></span><span class="token function">make</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'minutes'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 在创建响应之前设置 cookie</span>
</span><span class="code-line"><span class="token scope">Cookie<span class="token punctuation">::</span></span><span class="token function">queue</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'minutes'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 忘记cookie</span>
</span><span class="code-line"><span class="token scope">Cookie<span class="token punctuation">::</span></span><span class="token function">forget</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 发送带有响应的 cookie</span>
</span><span class="code-line"><span class="token variable">$response</span> <span class="token operator">=</span> <span class="token scope">Response<span class="token punctuation">::</span></span><span class="token function">make</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello World'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 将 cookie 添加到响应中</span>
</span><span class="code-line"><span class="token variable">$response</span><span class="token operator">-></span><span class="token function">withCookie</span><span class="token punctuation">(</span><span class="token scope">Cookie<span class="token punctuation">::</span></span><span class="token function">make</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">,</span> <span class="token variable">$minutes</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="unittest"><a aria-hidden="true" tabindex="-1" href="#unittest"><span class="icon icon-link"></span></a>UnitTest</h3><div class="wrap-body">
<p>安装并运行</p>
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token comment">// 添加到作曲家并更新：</span>
</span><span class="code-line"><span class="token string double-quoted-string">"phpunit/phpunit"</span><span class="token punctuation">:</span> <span class="token string double-quoted-string">"4.0.*"</span>
</span><span class="code-line"><span class="token comment">// 运行测试（从项目根目录）</span>
</span><span class="code-line"><span class="token operator">.</span><span class="token operator">/</span>vendor<span class="token operator">/</span>bin<span class="token operator">/</span>phpunit
</span></code></pre>
<p>断言</p>
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token this keyword">$this</span><span class="token operator">-></span><span class="token function">assertTrue</span><span class="token punctuation">(</span><span class="token constant boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token this keyword">$this</span><span class="token operator">-></span><span class="token function">assertEquals</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">,</span> <span class="token variable">$bar</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token this keyword">$this</span><span class="token operator">-></span><span class="token function">assertCount</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token variable">$times</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token this keyword">$this</span><span class="token operator">-></span><span class="token function">assertResponseOk</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token this keyword">$this</span><span class="token operator">-></span><span class="token function">assertResponseStatus</span><span class="token punctuation">(</span><span class="token number">403</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token this keyword">$this</span><span class="token operator">-></span><span class="token function">assertRedirectedTo</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token this keyword">$this</span><span class="token operator">-></span><span class="token function">assertRedirectedToRoute</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'route.name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token this keyword">$this</span><span class="token operator">-></span><span class="token function">assertRedirectedToAction</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Controller@method'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token this keyword">$this</span><span class="token operator">-></span><span class="token function">assertViewHas</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token this keyword">$this</span><span class="token operator">-></span><span class="token function">assertViewHas</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'age'</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token this keyword">$this</span><span class="token operator">-></span><span class="token function">assertSessionHasErrors</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 断言会话有给定键的错误...</span>
</span><span class="code-line"><span class="token this keyword">$this</span><span class="token operator">-></span><span class="token function">assertSessionHasErrors</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 断言会话有几个键的错误...</span>
</span><span class="code-line"><span class="token this keyword">$this</span><span class="token operator">-></span><span class="token function">assertSessionHasErrors</span><span class="token punctuation">(</span><span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'age'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token this keyword">$this</span><span class="token operator">-></span><span class="token function">assertHasOldInput</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
<p>调用路由</p>
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token variable">$response</span> <span class="token operator">=</span> <span class="token this keyword">$this</span><span class="token operator">-></span><span class="token function">call</span><span class="token punctuation">(</span><span class="token variable">$method</span><span class="token punctuation">,</span> <span class="token variable">$uri</span><span class="token punctuation">,</span> <span class="token variable">$parameters</span><span class="token punctuation">,</span> <span class="token variable">$files</span><span class="token punctuation">,</span> <span class="token variable">$server</span><span class="token punctuation">,</span> <span class="token variable">$content</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$response</span> <span class="token operator">=</span> <span class="token this keyword">$this</span><span class="token operator">-></span><span class="token function">callSecure</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'GET'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'foo/bar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token this keyword">$this</span><span class="token operator">-></span><span class="token function">session</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'foo'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'bar'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token this keyword">$this</span><span class="token operator">-></span><span class="token function">flushSession</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token this keyword">$this</span><span class="token operator">-></span><span class="token function">seed</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token this keyword">$this</span><span class="token operator">-></span><span class="token function">seed</span><span class="token punctuation">(</span><span class="token variable">$connection</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="cache"><a aria-hidden="true" tabindex="-1" href="#cache"><span class="icon icon-link"></span></a>Cache</h3><div class="wrap-body">
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token scope">Cache<span class="token punctuation">::</span></span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">,</span> <span class="token variable">$minutes</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Cache<span class="token punctuation">::</span></span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">,</span> <span class="token variable">$minutes</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Cache<span class="token punctuation">::</span></span><span class="token function">forever</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Cache<span class="token punctuation">::</span></span><span class="token function">remember</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span> <span class="token variable">$minutes</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token string single-quoted-string">'value'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Cache<span class="token punctuation">::</span></span><span class="token function">rememberForever</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token string single-quoted-string">'value'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Cache<span class="token punctuation">::</span></span><span class="token function">forget</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Cache<span class="token punctuation">::</span></span><span class="token function">has</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Cache<span class="token punctuation">::</span></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Cache<span class="token punctuation">::</span></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'default'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Cache<span class="token punctuation">::</span></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token string single-quoted-string">'default'</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Cache<span class="token punctuation">::</span></span><span class="token function">tags</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'my-tag'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span><span class="token string single-quoted-string">'value'</span><span class="token punctuation">,</span> <span class="token variable">$minutes</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Cache<span class="token punctuation">::</span></span><span class="token function">tags</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'my-tag'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">has</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Cache<span class="token punctuation">::</span></span><span class="token function">tags</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'my-tag'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Cache<span class="token punctuation">::</span></span><span class="token function">tags</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'my-tag'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">forget</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Cache<span class="token punctuation">::</span></span><span class="token function">tags</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'my-tag'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">flush</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Cache<span class="token punctuation">::</span></span><span class="token function">increment</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Cache<span class="token punctuation">::</span></span><span class="token function">increment</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span> <span class="token variable">$amount</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Cache<span class="token punctuation">::</span></span><span class="token function">decrement</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Cache<span class="token punctuation">::</span></span><span class="token function">decrement</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span> <span class="token variable">$amount</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Cache<span class="token punctuation">::</span></span><span class="token function">section</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'group'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Cache<span class="token punctuation">::</span></span><span class="token function">section</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'group'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Cache<span class="token punctuation">::</span></span><span class="token function">section</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'group'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">flush</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="session"><a aria-hidden="true" tabindex="-1" href="#session"><span class="icon icon-link"></span></a>Session</h3><div class="wrap-body">
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token scope">Session<span class="token punctuation">::</span></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 从会话中返回一个项目</span>
</span><span class="code-line"><span class="token scope">Session<span class="token punctuation">::</span></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'default'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Session<span class="token punctuation">::</span></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token string single-quoted-string">'default'</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 获取会话 ID</span>
</span><span class="code-line"><span class="token scope">Session<span class="token punctuation">::</span></span><span class="token function">getId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 在会话中放置一个键/值对</span>
</span><span class="code-line"><span class="token scope">Session<span class="token punctuation">::</span></span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 将值推送到会话中的数组中</span>
</span><span class="code-line"><span class="token scope">Session<span class="token punctuation">::</span></span><span class="token function">push</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo.bar'</span><span class="token punctuation">,</span><span class="token string single-quoted-string">'value'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 返回会话中的所有项目</span>
</span><span class="code-line"><span class="token scope">Session<span class="token punctuation">::</span></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 检查一个项目是否被定义</span>
</span><span class="code-line"><span class="token scope">Session<span class="token punctuation">::</span></span><span class="token function">has</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 从会话中删除项目</span>
</span><span class="code-line"><span class="token scope">Session<span class="token punctuation">::</span></span><span class="token function">forget</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 从会话中删除所有项目</span>
</span><span class="code-line"><span class="token scope">Session<span class="token punctuation">::</span></span><span class="token function">flush</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 生成新的会话标识符</span>
</span><span class="code-line"><span class="token scope">Session<span class="token punctuation">::</span></span><span class="token function">regenerate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 将键/值对闪存到会话</span>
</span><span class="code-line"><span class="token scope">Session<span class="token punctuation">::</span></span><span class="token function">flash</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 刷新所有会话闪存数据</span>
</span><span class="code-line"><span class="token scope">Session<span class="token punctuation">::</span></span><span class="token function">reflash</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 刷新当前闪存数据的一个子集</span>
</span><span class="code-line"><span class="token scope">Session<span class="token punctuation">::</span></span><span class="token function">keep</span><span class="token punctuation">(</span><span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key1'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'key2'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="response"><a aria-hidden="true" tabindex="-1" href="#response"><span class="icon icon-link"></span></a>Response</h3><div class="wrap-body">
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token keyword">return</span> <span class="token scope">Response<span class="token punctuation">::</span></span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$contents</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token keyword">return</span> <span class="token scope">Response<span class="token punctuation">::</span></span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$contents</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token keyword">return</span> <span class="token scope">Response<span class="token punctuation">::</span></span><span class="token function">json</span><span class="token punctuation">(</span><span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token keyword">return</span> <span class="token scope">Response<span class="token punctuation">::</span></span><span class="token function">json</span><span class="token punctuation">(</span><span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token operator">-></span><span class="token function">setCallback</span><span class="token punctuation">(</span><span class="token scope">Input<span class="token punctuation">::</span></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'callback'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token keyword">return</span> <span class="token scope">Response<span class="token punctuation">::</span></span><span class="token function">download</span><span class="token punctuation">(</span><span class="token variable">$filepath</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token keyword">return</span> <span class="token scope">Response<span class="token punctuation">::</span></span><span class="token function">download</span><span class="token punctuation">(</span><span class="token variable">$filepath</span><span class="token punctuation">,</span> <span class="token variable">$filename</span><span class="token punctuation">,</span> <span class="token variable">$headers</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 创建响应并修改标头值</span>
</span><span class="code-line"><span class="token variable">$response</span> <span class="token operator">=</span> <span class="token scope">Response<span class="token punctuation">::</span></span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$contents</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$response</span><span class="token operator">-></span><span class="token function">header</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Content-Type'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'application/json'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token keyword">return</span> <span class="token variable">$response</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 将 cookie 附加到响应</span>
</span><span class="code-line"><span class="token keyword">return</span> <span class="token scope">Response<span class="token punctuation">::</span></span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$content</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token operator">-></span><span class="token function">withCookie</span><span class="token punctuation">(</span><span class="token scope">Cookie<span class="token punctuation">::</span></span><span class="token function">make</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="request"><a aria-hidden="true" tabindex="-1" href="#request"><span class="icon icon-link"></span></a>Request</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token comment">// url: http://xx.com/aa/bb</span>
</span><span class="code-line"><span class="token scope">Request<span class="token punctuation">::</span></span><span class="token function">url</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 路径：/aa/bb</span>
</span><span class="code-line"><span class="token scope">Request<span class="token punctuation">::</span></span><span class="token function">path</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// getRequestUri: /aa/bb/?c=d</span>
</span><span class="code-line"><span class="token scope">Request<span class="token punctuation">::</span></span><span class="token function">getRequestUri</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 返回用户的IP</span>
</span><span class="code-line"><span class="token scope">Request<span class="token punctuation">::</span></span><span class="token function">getClientIp</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// getUri: http://xx.com/aa/bb/?c=d</span>
</span><span class="code-line"><span class="token scope">Request<span class="token punctuation">::</span></span><span class="token function">getUri</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 获取查询字符串：c=d</span>
</span><span class="code-line"><span class="token scope">Request<span class="token punctuation">::</span></span><span class="token function">getQueryString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 获取请求的端口方案（例如 80、443 等）</span>
</span><span class="code-line"><span class="token scope">Request<span class="token punctuation">::</span></span><span class="token function">getPort</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 确定当前请求 URI 是否与模式匹配</span>
</span><span class="code-line"><span class="token scope">Request<span class="token punctuation">::</span></span><span class="token function">is</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo/*'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 从 URI 中获取一个段（基于 1 的索引）</span>
</span><span class="code-line"><span class="token scope">Request<span class="token punctuation">::</span></span><span class="token function">segment</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 从请求中检索标头</span>
</span><span class="code-line"><span class="token scope">Request<span class="token punctuation">::</span></span><span class="token function">header</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Content-Type'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 从请求中检索服务器变量</span>
</span><span class="code-line"><span class="token scope">Request<span class="token punctuation">::</span></span><span class="token function">server</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'PATH_INFO'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 确定请求是否是 AJAX 调用的结果</span>
</span><span class="code-line"><span class="token scope">Request<span class="token punctuation">::</span></span><span class="token function">ajax</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 确定请求是否通过 HTTPS</span>
</span><span class="code-line"><span class="token scope">Request<span class="token punctuation">::</span></span><span class="token function">secure</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 获取请求方法</span>
</span><span class="code-line"><span class="token scope">Request<span class="token punctuation">::</span></span><span class="token function">method</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 检查请求方法是否为指定类型</span>
</span><span class="code-line"><span class="token scope">Request<span class="token punctuation">::</span></span><span class="token function">isMethod</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'post'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 获取原始 POST 数据</span>
</span><span class="code-line"><span class="token scope">Request<span class="token punctuation">::</span></span><span class="token function">instance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">getContent</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 获取请求的响应格式</span>
</span><span class="code-line"><span class="token scope">Request<span class="token punctuation">::</span></span><span class="token function">format</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 如果 HTTP Content-Type 标头包含 */json，则为真</span>
</span><span class="code-line"><span class="token scope">Request<span class="token punctuation">::</span></span><span class="token function">isJson</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 如果 HTTP Accept 标头是 application/json，则为真</span>
</span><span class="code-line"><span class="token scope">Request<span class="token punctuation">::</span></span><span class="token function">wantsJson</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="container"><a aria-hidden="true" tabindex="-1" href="#container"><span class="icon icon-link"></span></a>Container</h3><div class="wrap-body">
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token scope">App<span class="token punctuation">::</span></span><span class="token function">bind</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token variable">$app</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Foo</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">App<span class="token punctuation">::</span></span><span class="token function">make</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 如果这个类存在，则返回</span>
</span><span class="code-line"><span class="token scope">App<span class="token punctuation">::</span></span><span class="token function">make</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'FooBar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 在容器中注册共享绑定</span>
</span><span class="code-line"><span class="token scope">App<span class="token punctuation">::</span></span><span class="token function">singleton</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Foo</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 将现有实例注册为在容器中共享</span>
</span><span class="code-line"><span class="token scope">App<span class="token punctuation">::</span></span><span class="token function">instance</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">Foo</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 注册与容器的绑定</span>
</span><span class="code-line"><span class="token scope">App<span class="token punctuation">::</span></span><span class="token function">bind</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'FooRepositoryInterface'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'BarRepository'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 使用应用程序注册服务提供商</span>
</span><span class="code-line"><span class="token scope">App<span class="token punctuation">::</span></span><span class="token function">register</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'FooServiceProvider'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 监听对象分辨率</span>
</span><span class="code-line"><span class="token scope">App<span class="token punctuation">::</span></span><span class="token function">resolving</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token variable">$object</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="redirect"><a aria-hidden="true" tabindex="-1" href="#redirect"><span class="icon icon-link"></span></a>Redirect</h3><div class="wrap-body">
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token keyword">return</span> <span class="token scope">Redirect<span class="token punctuation">::</span></span><span class="token function">to</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo/bar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token keyword">return</span> <span class="token scope">Redirect<span class="token punctuation">::</span></span><span class="token function">to</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo/bar'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">with</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token keyword">return</span> <span class="token scope">Redirect<span class="token punctuation">::</span></span><span class="token function">to</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo/bar'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">withInput</span><span class="token punctuation">(</span><span class="token scope">Input<span class="token punctuation">::</span></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token keyword">return</span> <span class="token scope">Redirect<span class="token punctuation">::</span></span><span class="token function">to</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo/bar'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">withInput</span><span class="token punctuation">(</span><span class="token scope">Input<span class="token punctuation">::</span></span><span class="token function">except</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'password'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token keyword">return</span> <span class="token scope">Redirect<span class="token punctuation">::</span></span><span class="token function">to</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo/bar'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">withErrors</span><span class="token punctuation">(</span><span class="token variable">$validator</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 创建对先前位置的新重定向响应</span>
</span><span class="code-line"><span class="token keyword">return</span> <span class="token scope">Redirect<span class="token punctuation">::</span></span><span class="token function">back</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 创建对命名路由的新重定向响应</span>
</span><span class="code-line"><span class="token keyword">return</span> <span class="token scope">Redirect<span class="token punctuation">::</span></span><span class="token function">route</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foobar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token keyword">return</span> <span class="token scope">Redirect<span class="token punctuation">::</span></span><span class="token function">route</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foobar'</span><span class="token punctuation">,</span> <span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'value'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token keyword">return</span> <span class="token scope">Redirect<span class="token punctuation">::</span></span><span class="token function">route</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foobar'</span><span class="token punctuation">,</span> <span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 创建对控制器操作的新重定向响应</span>
</span><span class="code-line"><span class="token keyword">return</span> <span class="token scope">Redirect<span class="token punctuation">::</span></span><span class="token function">action</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'FooController@index'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token keyword">return</span> <span class="token scope">Redirect<span class="token punctuation">::</span></span><span class="token function">action</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'FooController@baz'</span><span class="token punctuation">,</span> <span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'value'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token keyword">return</span> <span class="token scope">Redirect<span class="token punctuation">::</span></span><span class="token function">action</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'FooController@baz'</span><span class="token punctuation">,</span> <span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 如果未定义预期的重定向，则默认为 foo/bar。</span>
</span><span class="code-line"><span class="token keyword">return</span> <span class="token scope">Redirect<span class="token punctuation">::</span></span><span class="token function">intended</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo/bar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="security"><a aria-hidden="true" tabindex="-1" href="#security"><span class="icon icon-link"></span></a>Security</h3><div class="wrap-body">
<p>Hashing</p>
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token scope">Hash<span class="token punctuation">::</span></span><span class="token function">make</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'secretpassword'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Hash<span class="token punctuation">::</span></span><span class="token function">check</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'secretpassword'</span><span class="token punctuation">,</span> <span class="token variable">$hashedPassword</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Hash<span class="token punctuation">::</span></span><span class="token function">needsRehash</span><span class="token punctuation">(</span><span class="token variable">$hashedPassword</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
<p>Encryption</p>
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token scope">Crypt<span class="token punctuation">::</span></span><span class="token function">encrypt</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'secretstring'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Crypt<span class="token punctuation">::</span></span><span class="token function">decrypt</span><span class="token punctuation">(</span><span class="token variable">$encryptedString</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Crypt<span class="token punctuation">::</span></span><span class="token function">setMode</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'ctr'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Crypt<span class="token punctuation">::</span></span><span class="token function">setCipher</span><span class="token punctuation">(</span><span class="token variable">$cipher</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="queue"><a aria-hidden="true" tabindex="-1" href="#queue"><span class="icon icon-link"></span></a>Queue</h3><div class="wrap-body">
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token scope">Queue<span class="token punctuation">::</span></span><span class="token function">push</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'SendMail'</span><span class="token punctuation">,</span> <span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'message'</span> <span class="token operator">=></span> <span class="token variable">$message</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Queue<span class="token punctuation">::</span></span><span class="token function">push</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'SendEmail@send'</span><span class="token punctuation">,</span> <span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'message'</span> <span class="token operator">=></span> <span class="token variable">$message</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Queue<span class="token punctuation">::</span></span><span class="token function">push</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token variable">$job</span><span class="token punctuation">)</span> <span class="token keyword">use</span> <span class="token variable">$id</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 多个工人的相同有效载荷</span>
</span><span class="code-line"><span class="token scope">Queue<span class="token punctuation">::</span></span><span class="token function">bulk</span><span class="token punctuation">(</span><span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'SendEmail'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'NotifyUser'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token variable">$payload</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
<p>Starting the queue listener</p>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">php artisan queue:listen
</span><span class="code-line">php artisan queue:listen connection
</span><span class="code-line">php artisan queue:listen <span class="token parameter variable">--timeout</span><span class="token operator">=</span><span class="token number">60</span>
</span><span class="code-line"><span class="token comment"># 只处理队列中的第一个作业</span>
</span><span class="code-line">php artisan queue:work
</span><span class="code-line"><span class="token comment"># 以守护进程模式启动一个队列工作者</span>
</span><span class="code-line">php artisan queue:work <span class="token parameter variable">--daemon</span>
</span><span class="code-line"><span class="token comment"># 为失败的作业创建迁移文件</span>
</span><span class="code-line">php artisan queue:failed-table
</span><span class="code-line"><span class="token comment"># 列出失败的工作</span>
</span><span class="code-line">php artisan queue:failed
</span><span class="code-line"><span class="token comment"># 通过 id 删除失败的作业</span>
</span><span class="code-line">php artisan queue:forget <span class="token number">5</span>
</span><span class="code-line"><span class="token comment"># 删除所有失败的作业</span>
</span><span class="code-line">php artisan queue:flush
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="view"><a aria-hidden="true" tabindex="-1" href="#view"><span class="icon icon-link"></span></a>View</h3><div class="wrap-body">
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token scope">View<span class="token punctuation">::</span></span><span class="token function">make</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'path/to/view'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">View<span class="token punctuation">::</span></span><span class="token function">make</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo/bar'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">with</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">View<span class="token punctuation">::</span></span><span class="token function">make</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo/bar'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">withKey</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'value'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">View<span class="token punctuation">::</span></span><span class="token function">make</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo/bar'</span><span class="token punctuation">,</span> <span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">View<span class="token punctuation">::</span></span><span class="token function">exists</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo/bar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 在所有视图中共享一个值</span>
</span><span class="code-line"><span class="token scope">View<span class="token punctuation">::</span></span><span class="token function">share</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 嵌套视图</span>
</span><span class="code-line"><span class="token scope">View<span class="token punctuation">::</span></span><span class="token function">make</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo/bar'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">nest</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'foo/baz'</span><span class="token punctuation">,</span> <span class="token variable">$data</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 注册视图编辑器</span>
</span><span class="code-line"><span class="token scope">View<span class="token punctuation">::</span></span><span class="token function">composer</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'viewname'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token variable">$view</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 向作曲家注册多个视图</span>
</span><span class="code-line"><span class="token scope">View<span class="token punctuation">::</span></span><span class="token function">composer</span><span class="token punctuation">(</span><span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'view1'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'view2'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token variable">$view</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 注册作曲家类</span>
</span><span class="code-line"><span class="token scope">View<span class="token punctuation">::</span></span><span class="token function">composer</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'viewname'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'FooComposer'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">View<span class="token punctuation">::</span></span><span class="token function">creator</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'viewname'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token variable">$view</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="validation"><a aria-hidden="true" tabindex="-1" href="#validation"><span class="icon icon-link"></span></a>Validation</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token scope">Validator<span class="token punctuation">::</span></span><span class="token function">make</span><span class="token punctuation">(</span>
</span><span class="code-line"><span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Foo'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
</span><span class="code-line"><span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|in:Foo'</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Validator<span class="token punctuation">::</span></span><span class="token function">extend</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token variable">$attribute</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">,</span> <span class="token variable">$params</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Validator<span class="token punctuation">::</span></span><span class="token function">extend</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'FooValidator@validate'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Validator<span class="token punctuation">::</span></span><span class="token function">resolver</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token variable">$translator</span><span class="token punctuation">,</span> <span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token variable">$rules</span><span class="token punctuation">,</span> <span class="token variable">$msgs</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">{</span>
</span><span class="code-line"><span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">FooValidator</span><span class="token punctuation">(</span><span class="token variable">$translator</span><span class="token punctuation">,</span> <span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token variable">$rules</span><span class="token punctuation">,</span> <span class="token variable">$msgs</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
<h4 id="rules"><a aria-hidden="true" tabindex="-1" href="#rules"><span class="icon icon-link"></span></a>Rules</h4>
<ul class="cols-3">
<li>accepted</li>
<li>active_url</li>
<li>after:YYYY-MM-DD</li>
<li>before:YYYY-MM-DD</li>
<li>alpha</li>
<li>alpha_dash</li>
<li>alpha_num</li>
<li>array</li>
<li>between:1,10</li>
<li>confirmed</li>
<li>date</li>
<li>date_format:YYYY-MM-DD</li>
<li>different:fieldname</li>
<li>digits:value</li>
<li>digits_between:min,max</li>
<li>boolean</li>
<li>email</li>
<li>exists:table,column</li>
<li>image</li>
<li>in:foo,bar,...</li>
<li>not_in:foo,bar,...</li>
<li>integer</li>
<li>numeric</li>
<li>ip</li>
<li>max:value</li>
<li>min:value</li>
<li>mimes:jpeg,png</li>
<li>regex:[0-9]</li>
<li>required</li>
<li>required_if:field,value</li>
<li>required_with:foo,bar,...</li>
<li>required_with_all:foo,bar,...</li>
<li>required_without:foo,bar,...</li>
<li>required_without_all:foo,bar,...</li>
<li>same:field</li>
<li>size:value</li>
<li>timezone</li>
<li>unique:table,column,except,idColumn</li>
<li>url</li>
</ul>
<!--rehype:className=cols-3-->
</div></div></div><div class="wrap h3body-exist"><div class="wrap-header h3wrap"><h3 id="form"><a aria-hidden="true" tabindex="-1" href="#form"><span class="icon icon-link"></span></a>Form</h3><div class="wrap-body">
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token scope">Form<span class="token punctuation">::</span></span><span class="token function">open</span><span class="token punctuation">(</span><span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'url'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'foo/bar'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'method'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'PUT'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Form<span class="token punctuation">::</span></span><span class="token function">open</span><span class="token punctuation">(</span><span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'route'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'foo.bar'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Form<span class="token punctuation">::</span></span><span class="token function">open</span><span class="token punctuation">(</span><span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'route'</span> <span class="token operator">=></span> <span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo.bar'</span><span class="token punctuation">,</span> <span class="token variable">$parameter</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Form<span class="token punctuation">::</span></span><span class="token function">open</span><span class="token punctuation">(</span><span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'action'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'FooController@method'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Form<span class="token punctuation">::</span></span><span class="token function">open</span><span class="token punctuation">(</span><span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'action'</span> <span class="token operator">=></span> <span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'FooController@method'</span><span class="token punctuation">,</span> <span class="token variable">$parameter</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Form<span class="token punctuation">::</span></span><span class="token function">open</span><span class="token punctuation">(</span><span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'url'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'foo/bar'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'files'</span> <span class="token operator">=></span> <span class="token constant boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Form<span class="token punctuation">::</span></span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Form<span class="token punctuation">::</span></span><span class="token function">token</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Form<span class="token punctuation">::</span></span><span class="token function">model</span><span class="token punctuation">(</span><span class="token variable">$foo</span><span class="token punctuation">,</span> <span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'route'</span> <span class="token operator">=></span> <span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo.bar'</span><span class="token punctuation">,</span> <span class="token variable">$foo</span><span class="token operator">-></span><span class="token property">bar</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
<h4 id="form-elements"><a aria-hidden="true" tabindex="-1" href="#form-elements"><span class="icon icon-link"></span></a>Form Elements</h4>
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token scope">Form<span class="token punctuation">::</span></span><span class="token function">label</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Description'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Form<span class="token punctuation">::</span></span><span class="token function">label</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Description'</span><span class="token punctuation">,</span> <span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'foo'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Form<span class="token punctuation">::</span></span><span class="token function">text</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Form<span class="token punctuation">::</span></span><span class="token function">text</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Form<span class="token punctuation">::</span></span><span class="token function">text</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">,</span> <span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Form<span class="token punctuation">::</span></span><span class="token function">textarea</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Form<span class="token punctuation">::</span></span><span class="token function">textarea</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Form<span class="token punctuation">::</span></span><span class="token function">textarea</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">,</span> <span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Form<span class="token punctuation">::</span></span><span class="token function">hidden</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Form<span class="token punctuation">::</span></span><span class="token function">password</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'password'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Form<span class="token punctuation">::</span></span><span class="token function">password</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'password'</span><span class="token punctuation">,</span> <span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'placeholder'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Password'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Form<span class="token punctuation">::</span></span><span class="token function">email</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">,</span> <span class="token keyword">array</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Form<span class="token punctuation">::</span></span><span class="token function">file</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Form<span class="token punctuation">::</span></span><span class="token function">checkbox</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 生成一个被选中的复选框</span>
</span><span class="code-line"><span class="token scope">Form<span class="token punctuation">::</span></span><span class="token function">checkbox</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">,</span> <span class="token constant boolean">true</span><span class="token punctuation">,</span> <span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Form<span class="token punctuation">::</span></span><span class="token function">radio</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 生成选定的无线电输入</span>
</span><span class="code-line"><span class="token scope">Form<span class="token punctuation">::</span></span><span class="token function">radio</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">,</span> <span class="token constant boolean">true</span><span class="token punctuation">,</span> <span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Form<span class="token punctuation">::</span></span><span class="token function">select</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Form<span class="token punctuation">::</span></span><span class="token function">select</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span> <span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Form<span class="token punctuation">::</span></span><span class="token function">selectRange</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'range'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Form<span class="token punctuation">::</span></span><span class="token function">selectYear</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'year'</span><span class="token punctuation">,</span> <span class="token number">2011</span><span class="token punctuation">,</span> <span class="token number">2015</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Form<span class="token punctuation">::</span></span><span class="token function">selectMonth</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'month'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Form<span class="token punctuation">::</span></span><span class="token function">submit</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Submit!'</span><span class="token punctuation">,</span> <span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Form<span class="token punctuation">::</span></span><span class="token function">button</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Form<span class="token punctuation">::</span></span><span class="token function">macro</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'fooField'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">{</span>
</span><span class="code-line"><span class="token keyword">return</span> <span class="token string single-quoted-string">'&#x3C;input type="custom"/>'</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Form<span class="token punctuation">::</span></span><span class="token function">fooField</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="string"><a aria-hidden="true" tabindex="-1" href="#string"><span class="icon icon-link"></span></a>String</h3><div class="wrap-body">
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token comment">// 将 UTF-8 值音译为 ASCII</span>
</span><span class="code-line"><span class="token scope">Str<span class="token punctuation">::</span></span><span class="token function">ascii</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token scope">Str<span class="token punctuation">::</span></span><span class="token function">camel</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token scope">Str<span class="token punctuation">::</span></span><span class="token function">contains</span><span class="token punctuation">(</span><span class="token variable">$haystack</span><span class="token punctuation">,</span> <span class="token variable">$needle</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token scope">Str<span class="token punctuation">::</span></span><span class="token function">endsWith</span><span class="token punctuation">(</span><span class="token variable">$haystack</span><span class="token punctuation">,</span> <span class="token variable">$needles</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// 用给定值的单个实例来限制字符串。</span>
</span><span class="code-line"><span class="token scope">Str<span class="token punctuation">::</span></span><span class="token function">finish</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">,</span> <span class="token variable">$cap</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token scope">Str<span class="token punctuation">::</span></span><span class="token function">is</span><span class="token punctuation">(</span><span class="token variable">$pattern</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token scope">Str<span class="token punctuation">::</span></span><span class="token function">length</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token scope">Str<span class="token punctuation">::</span></span><span class="token function">limit</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">,</span> <span class="token variable">$limit</span> <span class="token operator">=</span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token variable">$end</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'...'</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token scope">Str<span class="token punctuation">::</span></span><span class="token function">lower</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token scope">Str<span class="token punctuation">::</span></span><span class="token function">words</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">,</span> <span class="token variable">$words</span> <span class="token operator">=</span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token variable">$end</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'...'</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token scope">Str<span class="token punctuation">::</span></span><span class="token function">plural</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">,</span> <span class="token variable">$count</span> <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// 生成更真实的“随机”字母数字字符串。</span>
</span><span class="code-line"><span class="token scope">Str<span class="token punctuation">::</span></span><span class="token function">random</span><span class="token punctuation">(</span><span class="token variable">$length</span> <span class="token operator">=</span> <span class="token number">16</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// 生成“随机”字母数字字符串。</span>
</span><span class="code-line"><span class="token scope">Str<span class="token punctuation">::</span></span><span class="token function">quickRandom</span><span class="token punctuation">(</span><span class="token variable">$length</span> <span class="token operator">=</span> <span class="token number">16</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token scope">Str<span class="token punctuation">::</span></span><span class="token function">upper</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token scope">Str<span class="token punctuation">::</span></span><span class="token function">title</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token scope">Str<span class="token punctuation">::</span></span><span class="token function">singular</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token scope">Str<span class="token punctuation">::</span></span><span class="token function">slug</span><span class="token punctuation">(</span><span class="token variable">$title</span><span class="token punctuation">,</span> <span class="token variable">$separator</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'-'</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token scope">Str<span class="token punctuation">::</span></span><span class="token function">snake</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">,</span> <span class="token variable">$delimiter</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'_'</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token scope">Str<span class="token punctuation">::</span></span><span class="token function">startsWith</span><span class="token punctuation">(</span><span class="token variable">$haystack</span><span class="token punctuation">,</span> <span class="token variable">$needles</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// 将值转换为大写大小写。</span>
</span><span class="code-line"><span class="token scope">Str<span class="token punctuation">::</span></span><span class="token function">studly</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token scope">Str<span class="token punctuation">::</span></span><span class="token function">macro</span><span class="token punctuation">(</span><span class="token variable">$name</span><span class="token punctuation">,</span> <span class="token variable">$macro</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="blade"><a aria-hidden="true" tabindex="-1" href="#blade"><span class="icon icon-link"></span></a>Blade</h3><div class="wrap-body">
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token comment">// 在模板中显示一个部分</span>
</span><span class="code-line">@<span class="token keyword">yield</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span>
</span><span class="code-line">@<span class="token keyword">extends</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'layout.name'</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// 开始一个部分</span>
</span><span class="code-line">@<span class="token function">section</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// 结束一段</span>
</span><span class="code-line">@stop
</span><span class="code-line"><span class="token comment">// 结束一个部分并屈服</span>
</span><span class="code-line">@<span class="token function">section</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'sidebar'</span><span class="token punctuation">)</span>
</span><span class="code-line">@show
</span><span class="code-line">@<span class="token keyword">parent</span>
</span><span class="code-line">
</span><span class="code-line">@<span class="token keyword">include</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'view.name'</span><span class="token punctuation">)</span>
</span><span class="code-line">@<span class="token keyword">include</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'view.name'</span><span class="token punctuation">,</span> <span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">@<span class="token function">lang</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'messages.name'</span><span class="token punctuation">)</span>
</span><span class="code-line">@<span class="token function">choice</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'messages.name'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">
</span><span class="code-line">@<span class="token keyword">if</span>
</span><span class="code-line">@<span class="token keyword">else</span>
</span><span class="code-line">@<span class="token keyword">elseif</span>
</span><span class="code-line">@<span class="token keyword">endif</span>
</span><span class="code-line">
</span><span class="code-line">@unless
</span><span class="code-line">@endunless
</span><span class="code-line">
</span><span class="code-line">@<span class="token keyword">for</span>
</span><span class="code-line">@<span class="token keyword">endfor</span>
</span><span class="code-line">
</span><span class="code-line">@<span class="token keyword">foreach</span>
</span><span class="code-line">@<span class="token keyword">endforeach</span>
</span><span class="code-line">
</span><span class="code-line">@<span class="token keyword">while</span>
</span><span class="code-line">@<span class="token keyword">endwhile</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// 预测 4.2 功能</span>
</span><span class="code-line">@<span class="token function">forelse</span><span class="token punctuation">(</span><span class="token variable">$users</span> <span class="token keyword">as</span> <span class="token variable">$user</span><span class="token punctuation">)</span>
</span><span class="code-line">@<span class="token keyword">empty</span>
</span><span class="code-line">@endforelse
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// Echo 内容</span>
</span><span class="code-line"><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$var</span> <span class="token punctuation">}</span><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment">// Echo 转义内容</span>
</span><span class="code-line"><span class="token punctuation">{</span><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$var</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment">// Echo 未转义的内容； 5.0 功能</span>
</span><span class="code-line"><span class="token punctuation">{</span><span class="token operator">!</span><span class="token operator">!</span> <span class="token variable">$var</span> <span class="token operator">!</span><span class="token operator">!</span><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token punctuation">{</span><span class="token punctuation">{</span><span class="token operator">--</span> Blade Comment <span class="token operator">--</span><span class="token punctuation">}</span><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment">// 检查存在后 Echo 数据</span>
</span><span class="code-line"><span class="token punctuation">{</span><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$name</span> <span class="token keyword">or</span> <span class="token string single-quoted-string">'Default'</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment">// 显示带有花括号的原始文本</span>
</span><span class="code-line">@<span class="token punctuation">{</span><span class="token punctuation">{</span> This will not be processed by Blade <span class="token punctuation">}</span><span class="token punctuation">}</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="html"><a aria-hidden="true" tabindex="-1" href="#html"><span class="icon icon-link"></span></a>HTML</h3><div class="wrap-body">
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token scope">HTML<span class="token punctuation">::</span></span><span class="token function">macro</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 将 HTML 字符串转换为实体</span>
</span><span class="code-line"><span class="token scope">HTML<span class="token punctuation">::</span></span><span class="token function">entities</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 将实体转换为 HTML 字符</span>
</span><span class="code-line"><span class="token scope">HTML<span class="token punctuation">::</span></span><span class="token function">decode</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 生成指向 JavaScript 文件的链接</span>
</span><span class="code-line"><span class="token scope">HTML<span class="token punctuation">::</span></span><span class="token function">script</span><span class="token punctuation">(</span><span class="token variable">$url</span><span class="token punctuation">,</span> <span class="token variable">$attributes</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 生成指向 CSS 文件的链接</span>
</span><span class="code-line"><span class="token scope">HTML<span class="token punctuation">::</span></span><span class="token function">style</span><span class="token punctuation">(</span><span class="token variable">$url</span><span class="token punctuation">,</span> <span class="token variable">$attributes</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 生成 HTML 图像元素</span>
</span><span class="code-line"><span class="token scope">HTML<span class="token punctuation">::</span></span><span class="token function">image</span><span class="token punctuation">(</span><span class="token variable">$url</span><span class="token punctuation">,</span> <span class="token variable">$alt</span><span class="token punctuation">,</span> <span class="token variable">$attributes</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 生成 HTML 链接</span>
</span><span class="code-line"><span class="token scope">HTML<span class="token punctuation">::</span></span><span class="token function">link</span><span class="token punctuation">(</span><span class="token variable">$url</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'title'</span><span class="token punctuation">,</span> <span class="token variable">$attributes</span><span class="token punctuation">,</span> <span class="token variable">$secure</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 生成 HTTPS HTML 链接</span>
</span><span class="code-line"><span class="token scope">HTML<span class="token punctuation">::</span></span><span class="token function">secureLink</span><span class="token punctuation">(</span><span class="token variable">$url</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'title'</span><span class="token punctuation">,</span> <span class="token variable">$attributes</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 生成资产的 HTML 链接</span>
</span><span class="code-line"><span class="token scope">HTML<span class="token punctuation">::</span></span><span class="token function">linkAsset</span><span class="token punctuation">(</span><span class="token variable">$url</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'title'</span><span class="token punctuation">,</span> <span class="token variable">$attributes</span><span class="token punctuation">,</span> <span class="token variable">$secure</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 生成指向资产的 HTTPS HTML 链接</span>
</span><span class="code-line"><span class="token scope">HTML<span class="token punctuation">::</span></span><span class="token function">linkSecureAsset</span><span class="token punctuation">(</span><span class="token variable">$url</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'title'</span><span class="token punctuation">,</span> <span class="token variable">$attributes</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 生成指向命名路由的 HTML 链接</span>
</span><span class="code-line"><span class="token scope">HTML<span class="token punctuation">::</span></span><span class="token function">linkRoute</span><span class="token punctuation">(</span><span class="token variable">$name</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'title'</span><span class="token punctuation">,</span> <span class="token variable">$parameters</span><span class="token punctuation">,</span> <span class="token variable">$attributes</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 生成指向控制器操作的 HTML 链接</span>
</span><span class="code-line"><span class="token scope">HTML<span class="token punctuation">::</span></span><span class="token function">linkAction</span><span class="token punctuation">(</span><span class="token variable">$action</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'title'</span><span class="token punctuation">,</span> <span class="token variable">$parameters</span><span class="token punctuation">,</span> <span class="token variable">$attributes</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 生成指向电子邮件地址的 HTML 链接</span>
</span><span class="code-line"><span class="token scope">HTML<span class="token punctuation">::</span></span><span class="token function">mailto</span><span class="token punctuation">(</span><span class="token variable">$email</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'title'</span><span class="token punctuation">,</span> <span class="token variable">$attributes</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 混淆电子邮件地址以防止垃圾邮件机器人嗅探它</span>
</span><span class="code-line"><span class="token scope">HTML<span class="token punctuation">::</span></span><span class="token function">email</span><span class="token punctuation">(</span><span class="token variable">$email</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 生成有序的项目列表</span>
</span><span class="code-line"><span class="token scope">HTML<span class="token punctuation">::</span></span><span class="token function">ol</span><span class="token punctuation">(</span><span class="token variable">$list</span><span class="token punctuation">,</span> <span class="token variable">$attributes</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 生成一个未排序的项目列表</span>
</span><span class="code-line"><span class="token scope">HTML<span class="token punctuation">::</span></span><span class="token function">ul</span><span class="token punctuation">(</span><span class="token variable">$list</span><span class="token punctuation">,</span> <span class="token variable">$attributes</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 创建一个列表 HTML 元素</span>
</span><span class="code-line"><span class="token scope">HTML<span class="token punctuation">::</span></span><span class="token function">listing</span><span class="token punctuation">(</span><span class="token variable">$type</span><span class="token punctuation">,</span> <span class="token variable">$list</span><span class="token punctuation">,</span> <span class="token variable">$attributes</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 为列表元素创建 HTML</span>
</span><span class="code-line"><span class="token scope">HTML<span class="token punctuation">::</span></span><span class="token function">listingElement</span><span class="token punctuation">(</span><span class="token variable">$key</span><span class="token punctuation">,</span> <span class="token variable">$type</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 为嵌套列表属性创建 HTML</span>
</span><span class="code-line"><span class="token scope">HTML<span class="token punctuation">::</span></span><span class="token function">nestedListing</span><span class="token punctuation">(</span><span class="token variable">$key</span><span class="token punctuation">,</span> <span class="token variable">$type</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 从数组构建 HTML 属性字符串</span>
</span><span class="code-line"><span class="token scope">HTML<span class="token punctuation">::</span></span><span class="token function">attributes</span><span class="token punctuation">(</span><span class="token variable">$attributes</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 构建单个属性元素</span>
</span><span class="code-line"><span class="token scope">HTML<span class="token punctuation">::</span></span><span class="token function">attributeElement</span><span class="token punctuation">(</span><span class="token variable">$key</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 混淆字符串以防止垃圾邮件机器人嗅探它</span>
</span><span class="code-line"><span class="token scope">HTML<span class="token punctuation">::</span></span><span class="token function">obfuscate</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="db"><a aria-hidden="true" tabindex="-1" href="#db"><span class="icon icon-link"></span></a>DB</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="基本数据库使用"><a aria-hidden="true" tabindex="-1" href="#基本数据库使用"><span class="icon icon-link"></span></a>基本数据库使用</h3><div class="wrap-body">
<pre class="wrap-text"><code class="language-php code-highlight"><span class="code-line"><span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">connection</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'connection_name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 运行选择查询</span>
</span><span class="code-line"><span class="token variable">$results</span> <span class="token operator">=</span> <span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">select</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'select * from users where id = ?'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$results</span> <span class="token operator">=</span> <span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">select</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'select * from users where id = :id'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 运行一般声明</span>
</span><span class="code-line"><span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">statement</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'drop table users'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 侦听查询事件</span>
</span><span class="code-line"><span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">listen</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token variable">$sql</span><span class="token punctuation">,</span> <span class="token variable">$bindings</span><span class="token punctuation">,</span> <span class="token variable">$time</span><span class="token punctuation">)</span><span class="token punctuation">{</span> code_here<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 数据库事务</span>
</span><span class="code-line"><span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">transaction</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">update</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'votes'</span> <span class="token operator">=></span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">  <span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'posts'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">delete</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">beginTransaction</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">rollback</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">commit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div><div class="wrap h3body-not-exist col-span-2 row-span-3"><div class="wrap-header h3wrap"><h3 id="查询生成器"><a aria-hidden="true" tabindex="-1" href="#查询生成器"><span class="icon icon-link"></span></a>查询生成器</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2 row-span-3-->
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token comment">// 从表中检索所有行</span>
</span><span class="code-line"><span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 表中的分块结果</span>
</span><span class="code-line"><span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">chunk</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token variable">$users</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$users</span> <span class="token keyword">as</span> <span class="token variable">$user</span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token punctuation">{</span>  
</span><span class="code-line"><span class="token comment">//</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 从表中检索单行</span>
</span><span class="code-line"><span class="token variable">$user</span> <span class="token operator">=</span> <span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'John'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">first</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">first</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 从一行中检索单个列</span>
</span><span class="code-line"><span class="token variable">$name</span> <span class="token operator">=</span> <span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'John'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">pluck</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">pluck</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'column'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 检索列值列表</span>
</span><span class="code-line"><span class="token variable">$roles</span> <span class="token operator">=</span> <span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'roles'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">lists</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'title'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$roles</span> <span class="token operator">=</span> <span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'roles'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">lists</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'title'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 指定 Select 子句</span>
</span><span class="code-line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">select</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'email'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">distinct</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">select</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name as user_name'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 将 Select 子句添加到现有查询</span>
</span><span class="code-line"><span class="token variable">$query</span> <span class="token operator">=</span> <span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">select</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token variable">$query</span><span class="token operator">-></span><span class="token function">addSelect</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'age'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 使用 Where 运算符</span>
</span><span class="code-line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'>'</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span>
</span><span class="code-line">              <span class="token operator">-></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'>'</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span>
</span><span class="code-line">              <span class="token operator">-></span><span class="token function">orWhere</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'John'</span><span class="token punctuation">)</span>
</span><span class="code-line">              <span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span>
</span><span class="code-line">              <span class="token operator">-></span><span class="token function">whereBetween</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span>
</span><span class="code-line">              <span class="token operator">-></span><span class="token function">whereNotBetween</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span>
</span><span class="code-line">              <span class="token operator">-></span><span class="token function">whereIn</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span>
</span><span class="code-line">              <span class="token operator">-></span><span class="token function">whereNotIn</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span>
</span><span class="code-line">              <span class="token operator">-></span><span class="token function">whereNull</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'updated_at'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">whereNotNull</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'column'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 动态 Where 子句</span>
</span><span class="code-line"><span class="token variable">$admin</span> <span class="token operator">=</span> <span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">whereId</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">first</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$john</span> <span class="token operator">=</span> <span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span>
</span><span class="code-line">              <span class="token operator">-></span><span class="token function">whereIdAndEmail</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'john@doe.com'</span><span class="token punctuation">)</span>
</span><span class="code-line">              <span class="token operator">-></span><span class="token function">first</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$jane</span> <span class="token operator">=</span> <span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span>
</span><span class="code-line">              <span class="token operator">-></span><span class="token function">whereNameOrAge</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Jane'</span><span class="token punctuation">,</span> <span class="token number">22</span><span class="token punctuation">)</span>
</span><span class="code-line">              <span class="token operator">-></span><span class="token function">first</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 排序依据、分组依据和拥有</span>
</span><span class="code-line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span>
</span><span class="code-line">              <span class="token operator">-></span><span class="token function">orderBy</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'desc'</span><span class="token punctuation">)</span>
</span><span class="code-line">              <span class="token operator">-></span><span class="token function">groupBy</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'count'</span><span class="token punctuation">)</span>
</span><span class="code-line">              <span class="token operator">-></span><span class="token function">having</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'count'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'>'</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span>
</span><span class="code-line">              <span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">orderBy</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'column'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">orderBy</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'column'</span><span class="token punctuation">,</span><span class="token string single-quoted-string">'desc'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">having</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'count'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'>'</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 偏移和限制</span>
</span><span class="code-line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">skip</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">take</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="joins"><a aria-hidden="true" tabindex="-1" href="#joins"><span class="icon icon-link"></span></a>Joins</h3><div class="wrap-body">
<p>基本加入声明</p>
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span>
</span><span class="code-line">      <span class="token operator">-></span><span class="token function">join</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'contacts'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'users.id'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'='</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'contacts.user_id'</span><span class="token punctuation">)</span>
</span><span class="code-line">      <span class="token operator">-></span><span class="token function">join</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'orders'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'users.id'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'='</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'orders.user_id'</span><span class="token punctuation">)</span>
</span><span class="code-line">      <span class="token operator">-></span><span class="token function">select</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users.id'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'contacts.phone'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'orders.price'</span><span class="token punctuation">)</span>
</span><span class="code-line">      <span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
<p>左连接语句</p>
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span>
</span><span class="code-line">    <span class="token operator">-></span><span class="token function">leftJoin</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'posts'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'users.id'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'='</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'posts.user_id'</span><span class="token punctuation">)</span>
</span><span class="code-line">    <span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// select * from name = 'John' or (votes > 100 and title &#x3C;> 'Admin')</span>
</span><span class="code-line"><span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span>
</span><span class="code-line">        <span class="token operator">-></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'='</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'John'</span><span class="token punctuation">)</span>
</span><span class="code-line">        <span class="token operator">-></span><span class="token function">orWhere</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token variable">$query</span><span class="token punctuation">)</span>
</span><span class="code-line">        <span class="token punctuation">{</span>
</span><span class="code-line">            <span class="token variable">$query</span><span class="token operator">-></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'>'</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span>
</span><span class="code-line">                  <span class="token operator">-></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'title'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'&#x3C;>'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Admin'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">        <span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">        <span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="aggregates"><a aria-hidden="true" tabindex="-1" href="#aggregates"><span class="icon icon-link"></span></a>Aggregates</h3><div class="wrap-body">
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">count</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$price</span> <span class="token operator">=</span> <span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'orders'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">max</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'price'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$price</span> <span class="token operator">=</span> <span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'orders'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">min</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'price'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$price</span> <span class="token operator">=</span> <span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'orders'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">avg</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'price'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$total</span> <span class="token operator">=</span> <span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">sum</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">remember</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">remember</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'cache-key-name'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">cacheTags</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'my-key'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">remember</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span>
</span><span class="code-line">    <span class="token operator">-></span><span class="token function">cacheTags</span><span class="token punctuation">(</span><span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'my-first-key'</span><span class="token punctuation">,</span><span class="token string single-quoted-string">'my-second-key'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</span><span class="code-line">    <span class="token operator">-></span><span class="token function">remember</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span>
</span><span class="code-line">    <span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="原始表达式"><a aria-hidden="true" tabindex="-1" href="#原始表达式"><span class="icon icon-link"></span></a>原始表达式</h3><div class="wrap-body">
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span>
</span><span class="code-line">        <span class="token operator">-></span><span class="token function">select</span><span class="token punctuation">(</span><span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">raw</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'count(*) as user_count, status'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</span><span class="code-line">        <span class="token operator">-></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'status'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'&#x3C;>'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span>
</span><span class="code-line">        <span class="token operator">-></span><span class="token function">groupBy</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'status'</span><span class="token punctuation">)</span>
</span><span class="code-line">        <span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 返回行</span>
</span><span class="code-line"><span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">select</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'select * from users where id = ?'</span><span class="token punctuation">,</span> <span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'value'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 返回 nr 个受影响的行</span>
</span><span class="code-line"><span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'insert into foo set bar=2'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">update</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'update foo set bar=2'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">delete</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'delete from bar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 返回无效</span>
</span><span class="code-line"><span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">statement</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'update foo set bar=2'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 语句中的原始表达式</span>
</span><span class="code-line"><span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span>
</span><span class="code-line">        <span class="token operator">-></span><span class="token function">select</span><span class="token punctuation">(</span><span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">raw</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'count(*) as count, column2'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</span><span class="code-line">        <span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="inserts-插入"><a aria-hidden="true" tabindex="-1" href="#inserts-插入"><span class="icon icon-link"></span></a>Inserts 插入</h3><div class="wrap-body">
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">insert</span><span class="token punctuation">(</span>
</span><span class="code-line">  <span class="token punctuation">[</span><span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'john@example.com'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'votes'</span> <span class="token operator">=></span> <span class="token number">0</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$id</span> <span class="token operator">=</span> <span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">insertGetId</span><span class="token punctuation">(</span>
</span><span class="code-line">  <span class="token punctuation">[</span><span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'john@example.com'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'votes'</span> <span class="token operator">=></span> <span class="token number">0</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token punctuation">[</span>
</span><span class="code-line">  <span class="token punctuation">[</span><span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'taylor@example.com'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'votes'</span> <span class="token operator">=></span> <span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
</span><span class="code-line">  <span class="token punctuation">[</span><span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'dayle@example.com'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'votes'</span> <span class="token operator">=></span> <span class="token number">0</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="updates-更新"><a aria-hidden="true" tabindex="-1" href="#updates-更新"><span class="icon icon-link"></span></a>Updates 更新</h3><div class="wrap-body">
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span>
</span><span class="code-line">          <span class="token operator">-></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span>
</span><span class="code-line">          <span class="token operator">-></span><span class="token function">update</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'votes'</span> <span class="token operator">=></span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">increment</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">increment</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">decrement</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">decrement</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">increment</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'John'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="deletes-删除"><a aria-hidden="true" tabindex="-1" href="#deletes-删除"><span class="icon icon-link"></span></a>Deletes 删除</h3><div class="wrap-body">
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'&#x3C;'</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">delete</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">delete</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">truncate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="unions-联合"><a aria-hidden="true" tabindex="-1" href="#unions-联合"><span class="icon icon-link"></span></a>Unions 联合</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token comment">// unionAll() 方法也可用，并且具有与 union 相同的方法签名</span>
</span><span class="code-line"><span class="token variable">$first</span> <span class="token operator">=</span> <span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">whereNull</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'first_name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">whereNull</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'last_name'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">union</span><span class="token punctuation">(</span><span class="token variable">$first</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// Pessimistic Locking 悲观锁定</span>
</span><span class="code-line"><span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'>'</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">sharedLock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'>'</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">lockForUpdate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="input"><a aria-hidden="true" tabindex="-1" href="#input"><span class="icon icon-link"></span></a>Input</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="input-1"><a aria-hidden="true" tabindex="-1" href="#input-1"><span class="icon icon-link"></span></a>Input</h3><div class="wrap-body">
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token scope">Input<span class="token punctuation">::</span></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 如果密钥丢失则默认</span>
</span><span class="code-line"><span class="token scope">Input<span class="token punctuation">::</span></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'default'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Input<span class="token punctuation">::</span></span><span class="token function">has</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Input<span class="token punctuation">::</span></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 获取输入时仅检索“foo”和“bar”</span>
</span><span class="code-line"><span class="token scope">Input<span class="token punctuation">::</span></span><span class="token function">only</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'bar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 获取输入时忽略“foo”</span>
</span><span class="code-line"><span class="token scope">Input<span class="token punctuation">::</span></span><span class="token function">except</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Input<span class="token punctuation">::</span></span><span class="token function">flush</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="session-input-flash"><a aria-hidden="true" tabindex="-1" href="#session-input-flash"><span class="icon icon-link"></span></a>Session Input (flash)</h3><div class="wrap-body">
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token comment">// 会话的闪存输入</span>
</span><span class="code-line"><span class="token scope">Input<span class="token punctuation">::</span></span><span class="token function">flash</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 只闪烁会话的一些输入</span>
</span><span class="code-line"><span class="token scope">Input<span class="token punctuation">::</span></span><span class="token function">flashOnly</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'bar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 只闪烁会话的一些输入</span>
</span><span class="code-line"><span class="token scope">Input<span class="token punctuation">::</span></span><span class="token function">flashExcept</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'baz'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 检索旧输入项</span>
</span><span class="code-line"><span class="token scope">Input<span class="token punctuation">::</span></span><span class="token function">old</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span><span class="token string single-quoted-string">'default_value'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="files"><a aria-hidden="true" tabindex="-1" href="#files"><span class="icon icon-link"></span></a>Files</h3><div class="wrap-body">
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token comment">// 使用已上传的文件</span>
</span><span class="code-line"><span class="token scope">Input<span class="token punctuation">::</span></span><span class="token function">file</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'filename'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 判断文件是否上传</span>
</span><span class="code-line"><span class="token scope">Input<span class="token punctuation">::</span></span><span class="token function">hasFile</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'filename'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 访问文件属性</span>
</span><span class="code-line"><span class="token scope">Input<span class="token punctuation">::</span></span><span class="token function">file</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">getRealPath</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Input<span class="token punctuation">::</span></span><span class="token function">file</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">getClientOriginalName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Input<span class="token punctuation">::</span></span><span class="token function">file</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">getClientOriginalExtension</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Input<span class="token punctuation">::</span></span><span class="token function">file</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">getSize</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Input<span class="token punctuation">::</span></span><span class="token function">file</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">getMimeType</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 移动上传的文件</span>
</span><span class="code-line"><span class="token scope">Input<span class="token punctuation">::</span></span><span class="token function">file</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">move</span><span class="token punctuation">(</span><span class="token variable">$destinationPath</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 移动上传的文件</span>
</span><span class="code-line"><span class="token scope">Input<span class="token punctuation">::</span></span><span class="token function">file</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">move</span><span class="token punctuation">(</span><span class="token variable">$destinationPath</span><span class="token punctuation">,</span> <span class="token variable">$fileName</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="路由"><a aria-hidden="true" tabindex="-1" href="#路由"><span class="icon icon-link"></span></a>路由</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="route"><a aria-hidden="true" tabindex="-1" href="#route"><span class="icon icon-link"></span></a>Route</h3><div class="wrap-body">
<pre class="wrap-text"><code class="language-php code-highlight"><span class="code-line"><span class="token scope">Route<span class="token punctuation">::</span></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Route<span class="token punctuation">::</span></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'ControllerName@function'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Route<span class="token punctuation">::</span></span><span class="token function">controller</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'FooController'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="restful-控制器"><a aria-hidden="true" tabindex="-1" href="#restful-控制器"><span class="icon icon-link"></span></a>RESTful 控制器</h3><div class="wrap-body">
<pre class="wrap-text"><code class="language-php code-highlight"><span class="code-line"><span class="token scope">Route<span class="token punctuation">::</span></span><span class="token function">resource</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'posts'</span><span class="token punctuation">,</span><span class="token string single-quoted-string">'PostsController'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 指定要在路线上处理的动作子集</span>
</span><span class="code-line"><span class="token scope">Route<span class="token punctuation">::</span></span><span class="token function">resource</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'photo'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'PhotoController'</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'only'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'index'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'show'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Route<span class="token punctuation">::</span></span><span class="token function">resource</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'photo'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'PhotoController'</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'except'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'update'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'destroy'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="触发错误"><a aria-hidden="true" tabindex="-1" href="#触发错误"><span class="icon icon-link"></span></a>触发错误</h3><div class="wrap-body">
<pre class="wrap-text"><code class="language-php code-highlight"><span class="code-line"><span class="token scope">App<span class="token punctuation">::</span></span><span class="token function">abort</span><span class="token punctuation">(</span><span class="token number">404</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$handler</span><span class="token operator">-></span><span class="token function">missing</span><span class="token punctuation">(</span><span class="token operator">...</span><span class="token punctuation">)</span> in <span class="token scope">ErrorServiceProvider<span class="token punctuation">::</span></span><span class="token function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">NotFoundHttpException</span><span class="token punctuation">;</span>
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="路由参数"><a aria-hidden="true" tabindex="-1" href="#路由参数"><span class="icon icon-link"></span></a>路由参数</h3><div class="wrap-body">
<pre class="wrap-text"><code class="language-php code-highlight"><span class="code-line"><span class="token scope">Route<span class="token punctuation">::</span></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo/{bar}'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token variable">$bar</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Route<span class="token punctuation">::</span></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo/{bar?}'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token variable">$bar</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'bar'</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="http-动词"><a aria-hidden="true" tabindex="-1" href="#http-动词"><span class="icon icon-link"></span></a>HTTP 动词</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="wrap-text"><code class="language-php code-highlight"><span class="code-line"><span class="token scope">Route<span class="token punctuation">::</span></span><span class="token function">any</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Route<span class="token punctuation">::</span></span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Route<span class="token punctuation">::</span></span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Route<span class="token punctuation">::</span></span><span class="token function">patch</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Route<span class="token punctuation">::</span></span><span class="token function">delete</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// RESTful 动作</span>
</span><span class="code-line"><span class="token scope">Route<span class="token punctuation">::</span></span><span class="token function">resource</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'FooController'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 为多个动词注册一个路由</span>
</span><span class="code-line"><span class="token scope">Route<span class="token punctuation">::</span></span><span class="token keyword">match</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'get'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'post'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'/'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="secure-routestbd"><a aria-hidden="true" tabindex="-1" href="#secure-routestbd"><span class="icon icon-link"></span></a>Secure Routes(TBD)</h3><div class="wrap-body">
<pre class="wrap-text"><code class="language-php code-highlight"><span class="code-line"><span class="token scope">Route<span class="token punctuation">::</span></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">,</span> <span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'https'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="路由约束"><a aria-hidden="true" tabindex="-1" href="#路由约束"><span class="icon icon-link"></span></a>路由约束</h3><div class="wrap-body">
<pre class="wrap-text"><code class="language-php code-highlight"><span class="code-line"><span class="token scope">Route<span class="token punctuation">::</span></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo/{bar}'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token variable">$bar</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token operator">-></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'bar'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'[0-9]+'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Route<span class="token punctuation">::</span></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo/{bar}/{baz}'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token variable">$bar</span><span class="token punctuation">,</span> <span class="token variable">$baz</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token operator">-></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'bar'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'[0-9]+'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'baz'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'[A-Za-z]'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// 设置跨路线使用的模式</span>
</span><span class="code-line"><span class="token scope">Route<span class="token punctuation">::</span></span><span class="token function">pattern</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'bar'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'[0-9]+'</span><span class="token punctuation">)</span>
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="http-中间件"><a aria-hidden="true" tabindex="-1" href="#http-中间件"><span class="icon icon-link"></span></a>HTTP 中间件</h3><div class="wrap-body">
<pre class="wrap-text"><code class="language-php code-highlight"><span class="code-line"><span class="token comment">// 为路由分配中间件</span>
</span><span class="code-line"><span class="token scope">Route<span class="token punctuation">::</span></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'admin/profile'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'middleware'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'auth'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="命名路由"><a aria-hidden="true" tabindex="-1" href="#命名路由"><span class="icon icon-link"></span></a>命名路由</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="wrap-text"><code class="language-php code-highlight"><span class="code-line"><span class="token scope">Route<span class="token punctuation">::</span></span><span class="token function">currentRouteName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Route<span class="token punctuation">::</span></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo/bar'</span><span class="token punctuation">,</span> <span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'as'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'foobar'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Route<span class="token punctuation">::</span></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user/profile'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span>
</span><span class="code-line">  <span class="token string single-quoted-string">'as'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'profile'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'uses'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'UserController@showProfile'</span>
</span><span class="code-line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">route</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'profile'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$redirect</span> <span class="token operator">=</span> <span class="token function">redirect</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">route</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'profile'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="路由前缀"><a aria-hidden="true" tabindex="-1" href="#路由前缀"><span class="icon icon-link"></span></a>路由前缀</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="wrap-text"><code class="language-php code-highlight"><span class="code-line"><span class="token scope">Route<span class="token punctuation">::</span></span><span class="token function">group</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'prefix'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'admin'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token scope">Route<span class="token punctuation">::</span></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
</span><span class="code-line">      <span class="token keyword">return</span> <span class="token string single-quoted-string">'Matches The "/admin/users" URL'</span><span class="token punctuation">;</span>
</span><span class="code-line">  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="路由命名空间"><a aria-hidden="true" tabindex="-1" href="#路由命名空间"><span class="icon icon-link"></span></a>路由命名空间</h3><div class="wrap-body">
<pre class="wrap-text"><code class="language-php code-highlight"><span class="code-line"><span class="token comment">// 该路由组将携带命名空间“Foo\Bar”</span>
</span><span class="code-line"><span class="token scope">Route<span class="token punctuation">::</span></span><span class="token function">group</span><span class="token punctuation">(</span><span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'namespace'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Foo\Bar'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="子域路由"><a aria-hidden="true" tabindex="-1" href="#子域路由"><span class="icon icon-link"></span></a>子域路由</h3><div class="wrap-body">
<pre class="wrap-text"><code class="language-php code-highlight"><span class="code-line"><span class="token comment">// {sub} 将被传递给闭包</span>
</span><span class="code-line"><span class="token scope">Route<span class="token punctuation">::</span></span><span class="token function">group</span><span class="token punctuation">(</span><span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'domain'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'{sub}.example.com'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="model"><a aria-hidden="true" tabindex="-1" href="#model"><span class="icon icon-link"></span></a>Model</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="基本用法"><a aria-hidden="true" tabindex="-1" href="#基本用法"><span class="icon icon-link"></span></a>基本用法</h3><div class="wrap-body">
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token comment">// 定义 Eloquent 模型</span>
</span><span class="code-line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">User</span> <span class="token keyword">extends</span> <span class="token class-name">Model</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment">// 生成 Eloquent 模型</span>
</span><span class="code-line">php artisan make<span class="token punctuation">:</span>model User
</span><span class="code-line"><span class="token comment">// 指定自定义表名</span>
</span><span class="code-line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">User</span> <span class="token keyword">extends</span> <span class="token class-name">Model</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">protected</span> <span class="token variable">$table</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'my_users'</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-4 col-span-2"><div class="wrap-header h3wrap"><h3 id="more"><a aria-hidden="true" tabindex="-1" href="#more"><span class="icon icon-link"></span></a>More</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-4 col-span-2-->
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token scope">Model<span class="token punctuation">::</span></span><span class="token function">create</span><span class="token punctuation">(</span><span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 按属性查找第一个匹配记录或创建</span>
</span><span class="code-line"><span class="token scope">Model<span class="token punctuation">::</span></span><span class="token function">firstOrCreate</span><span class="token punctuation">(</span><span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 按属性查找第一条记录或实例化</span>
</span><span class="code-line"><span class="token scope">Model<span class="token punctuation">::</span></span><span class="token function">firstOrNew</span><span class="token punctuation">(</span><span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 创建或更新匹配属性的记录，并填充值</span>
</span><span class="code-line"><span class="token scope">Model<span class="token punctuation">::</span></span><span class="token function">updateOrCreate</span><span class="token punctuation">(</span><span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'search_key'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'search_value'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 用属性数组填充模型，注意批量赋值！</span>
</span><span class="code-line"><span class="token scope">Model<span class="token punctuation">::</span></span><span class="token function">fill</span><span class="token punctuation">(</span><span class="token variable">$attributes</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Model<span class="token punctuation">::</span></span><span class="token function">destroy</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Model<span class="token punctuation">::</span></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Model<span class="token punctuation">::</span></span><span class="token function">find</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 使用双主键查找</span>
</span><span class="code-line"><span class="token scope">Model<span class="token punctuation">::</span></span><span class="token function">find</span><span class="token punctuation">(</span><span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'first'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'last'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 如果查找失败则抛出异常</span>
</span><span class="code-line"><span class="token scope">Model<span class="token punctuation">::</span></span><span class="token function">findOrFail</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 使用双主键查找并在查找失败时抛出异常</span>
</span><span class="code-line"><span class="token scope">Model<span class="token punctuation">::</span></span><span class="token function">findOrFail</span><span class="token punctuation">(</span><span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'first'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'last'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Model<span class="token punctuation">::</span></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'='</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'bar'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Model<span class="token punctuation">::</span></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'='</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'bar'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">first</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 动态的</span>
</span><span class="code-line"><span class="token scope">Model<span class="token punctuation">::</span></span><span class="token function">whereFoo</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'bar'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">first</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 如果查找失败则抛出异常</span>
</span><span class="code-line"><span class="token scope">Model<span class="token punctuation">::</span></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'='</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'bar'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">firstOrFail</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Model<span class="token punctuation">::</span></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'='</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'bar'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">count</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Model<span class="token punctuation">::</span></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'='</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'bar'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">delete</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 输出原始查询</span>
</span><span class="code-line"><span class="token scope">Model<span class="token punctuation">::</span></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'='</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'bar'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">toSql</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Model<span class="token punctuation">::</span></span><span class="token function">whereRaw</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo = bar and cars = 2'</span><span class="token punctuation">,</span> <span class="token keyword">array</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Model<span class="token punctuation">::</span></span><span class="token function">remember</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Model<span class="token punctuation">::</span></span><span class="token function">remember</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'cache-key-name'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Model<span class="token punctuation">::</span></span><span class="token function">cacheTags</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'my-tag'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">remember</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Model<span class="token punctuation">::</span></span><span class="token function">cacheTags</span><span class="token punctuation">(</span><span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'my-first-key'</span><span class="token punctuation">,</span><span class="token string single-quoted-string">'my-second-key'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">remember</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Model<span class="token punctuation">::</span></span><span class="token function">on</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'connection-name'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">find</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Model<span class="token punctuation">::</span></span><span class="token function">with</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'relation'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Model<span class="token punctuation">::</span></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">take</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Model<span class="token punctuation">::</span></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">skip</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 默认 Eloquent 排序方兴未艾</span>
</span><span class="code-line"><span class="token scope">Model<span class="token punctuation">::</span></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">orderBy</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'column'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Model<span class="token punctuation">::</span></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">orderBy</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'column'</span><span class="token punctuation">,</span><span class="token string single-quoted-string">'desc'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="soft-delete"><a aria-hidden="true" tabindex="-1" href="#soft-delete"><span class="icon icon-link"></span></a>Soft Delete</h3><div class="wrap-body">
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token scope">Model<span class="token punctuation">::</span></span><span class="token function">withTrashed</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'cars'</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 在结果中包含软删除模型</span>
</span><span class="code-line"><span class="token scope">Model<span class="token punctuation">::</span></span><span class="token function">withTrashed</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'cars'</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">restore</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Model<span class="token punctuation">::</span></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'cars'</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">forceDelete</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 强制结果集只包含软删除</span>
</span><span class="code-line"><span class="token scope">Model<span class="token punctuation">::</span></span><span class="token function">onlyTrashed</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'cars'</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="events"><a aria-hidden="true" tabindex="-1" href="#events"><span class="icon icon-link"></span></a>Events</h3><div class="wrap-body">
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token scope">Model<span class="token punctuation">::</span></span><span class="token function">creating</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token variable">$model</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Model<span class="token punctuation">::</span></span><span class="token function">created</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token variable">$model</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Model<span class="token punctuation">::</span></span><span class="token function">updating</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token variable">$model</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Model<span class="token punctuation">::</span></span><span class="token function">updated</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token variable">$model</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Model<span class="token punctuation">::</span></span><span class="token function">saving</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token variable">$model</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Model<span class="token punctuation">::</span></span><span class="token function">saved</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token variable">$model</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Model<span class="token punctuation">::</span></span><span class="token function">deleting</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token variable">$model</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Model<span class="token punctuation">::</span></span><span class="token function">deleted</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token variable">$model</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Model<span class="token punctuation">::</span></span><span class="token function">observe</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">FooObserver</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="eloquent-configuration"><a aria-hidden="true" tabindex="-1" href="#eloquent-configuration"><span class="icon icon-link"></span></a>Eloquent Configuration</h3><div class="wrap-body">
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token comment">// 禁止从模型插入和更新中抛出批量分配异常</span>
</span><span class="code-line"><span class="token scope">Eloquent<span class="token punctuation">::</span></span><span class="token function">unguard</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 启用任何抛出批量分配异常的能力</span>
</span><span class="code-line"><span class="token scope">Eloquent<span class="token punctuation">::</span></span><span class="token function">reguard</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="schema"><a aria-hidden="true" tabindex="-1" href="#schema"><span class="icon icon-link"></span></a>Schema</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist row-span-4"><div class="wrap-header h3wrap"><h3 id="schema-1"><a aria-hidden="true" tabindex="-1" href="#schema-1"><span class="icon icon-link"></span></a>Schema</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-4-->
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token comment">// 表示需要创建表</span>
</span><span class="code-line"><span class="token scope">Schema<span class="token punctuation">::</span></span><span class="token function">create</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'table'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token variable">$table</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">increments</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 指定连接</span>
</span><span class="code-line"><span class="token scope">Schema<span class="token punctuation">::</span></span><span class="token function">connection</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">create</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'table'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token variable">$table</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 将表重命名为给定名称</span>
</span><span class="code-line"><span class="token scope">Schema<span class="token punctuation">::</span></span><span class="token function">rename</span><span class="token punctuation">(</span><span class="token variable">$from</span><span class="token punctuation">,</span> <span class="token variable">$to</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 表示应该删除该表</span>
</span><span class="code-line"><span class="token scope">Schema<span class="token punctuation">::</span></span><span class="token function">drop</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'table'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 指示如果表存在则应将其删除</span>
</span><span class="code-line"><span class="token scope">Schema<span class="token punctuation">::</span></span><span class="token function">dropIfExists</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'table'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 确定给定表是否存在</span>
</span><span class="code-line"><span class="token scope">Schema<span class="token punctuation">::</span></span><span class="token function">hasTable</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'table'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 确定给定表是否具有给定列</span>
</span><span class="code-line"><span class="token scope">Schema<span class="token punctuation">::</span></span><span class="token function">hasColumn</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'table'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'column'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 更新现有表</span>
</span><span class="code-line"><span class="token scope">Schema<span class="token punctuation">::</span></span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'table'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token variable">$table</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 指示应重命名给定的列</span>
</span><span class="code-line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">renameColumn</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'from'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'to'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 指示应删除给定的列</span>
</span><span class="code-line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">dropColumn</span><span class="token punctuation">(</span><span class="token keyword type-declaration">string</span><span class="token operator">|</span><span class="token keyword type-declaration">array</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 应该用于表的存储引擎</span>
</span><span class="code-line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token property">engine</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'InnoDB'</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 仅适用于 MySQL</span>
</span><span class="code-line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token keyword type-declaration">string</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">after</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'email'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-3"><div class="wrap-header h3wrap"><h3 id="indexes"><a aria-hidden="true" tabindex="-1" href="#indexes"><span class="icon icon-link"></span></a>Indexes</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-3-->
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token keyword type-declaration">string</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'column'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">unique</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">primary</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'column'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 创建双主键</span>
</span><span class="code-line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">primary</span><span class="token punctuation">(</span><span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'first'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'last'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">unique</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'column'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">unique</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'column'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'key_name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 创建双唯一索引</span>
</span><span class="code-line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">unique</span><span class="token punctuation">(</span><span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'first'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'last'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">unique</span><span class="token punctuation">(</span><span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'first'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'last'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'key_name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">index</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'column'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">index</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'column'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'key_name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 创建双索引</span>
</span><span class="code-line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">index</span><span class="token punctuation">(</span><span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'first'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'last'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">index</span><span class="token punctuation">(</span><span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'first'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'last'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'key_name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">dropPrimary</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'table_column_primary'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">dropUnique</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'table_column_unique'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">dropIndex</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'table_column_index'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="foreign-keys"><a aria-hidden="true" tabindex="-1" href="#foreign-keys"><span class="icon icon-link"></span></a>Foreign Keys</h3><div class="wrap-body">
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">foreign</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user_id'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">references</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">on</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">foreign</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user_id'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">references</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">on</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">onDelete</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'cascade'</span><span class="token operator">|</span><span class="token string single-quoted-string">'restrict'</span><span class="token operator">|</span><span class="token string single-quoted-string">'set null'</span><span class="token operator">|</span><span class="token string single-quoted-string">'no action'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">foreign</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user_id'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">references</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">on</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">onUpdate</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'cascade'</span><span class="token operator">|</span><span class="token string single-quoted-string">'restrict'</span><span class="token operator">|</span><span class="token string single-quoted-string">'set null'</span><span class="token operator">|</span><span class="token string single-quoted-string">'no action'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">dropForeign</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'posts_user_id_foreign'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="列类型---增量"><a aria-hidden="true" tabindex="-1" href="#列类型---增量"><span class="icon icon-link"></span></a>列类型 - 增量</h3><div class="wrap-body">
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">increments</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">bigIncrements</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="列类型---字符串和文本"><a aria-hidden="true" tabindex="-1" href="#列类型---字符串和文本"><span class="icon icon-link"></span></a>列类型 - 字符串和文本</h3><div class="wrap-body">
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">char</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token keyword type-declaration">string</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'email'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token keyword type-declaration">string</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">text</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'description'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">mediumText</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'description'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">longText</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'description'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="列类型---数字"><a aria-hidden="true" tabindex="-1" href="#列类型---数字"><span class="icon icon-link"></span></a>列类型 - 数字</h3><div class="wrap-body">
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">integer</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">tinyInteger</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">smallInteger</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">mediumInteger</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">bigInteger</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token keyword type-declaration">float</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'amount'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">double</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'column'</span><span class="token punctuation">,</span> <span class="token number">15</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">decimal</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'amount'</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="列类型---日期和时间"><a aria-hidden="true" tabindex="-1" href="#列类型---日期和时间"><span class="icon icon-link"></span></a>列类型 - 日期和时间</h3><div class="wrap-body">
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">date</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'created_at'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">dateTime</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'created_at'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">time</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'sunrise'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">timestamp</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'added_on'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="列类型---添加-created_at-和-updated_at-列"><a aria-hidden="true" tabindex="-1" href="#列类型---添加-created_at-和-updated_at-列"><span class="icon icon-link"></span></a>列类型 - 添加 created_at 和 updated_at 列</h3><div class="wrap-body">
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">timestamps</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">nullableTimestamps</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="列类型---其他"><a aria-hidden="true" tabindex="-1" href="#列类型---其他"><span class="icon icon-link"></span></a>列类型 - 其他</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">binary</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'data'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">boolean</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'confirmed'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 为软删除添加 deleted_at 列</span>
</span><span class="code-line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">softDeletes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">enum</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'choices'</span><span class="token punctuation">,</span> <span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'bar'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 添加 remember_token 作为 VARCHAR(100) NULL</span>
</span><span class="code-line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">rememberToken</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 添加 INTEGER parent_id 和 STRING parent_type</span>
</span><span class="code-line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">morphs</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'parent'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token operator">-></span><span class="token function">nullable</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token operator">-></span><span class="token function">default</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token operator">-></span><span class="token function">unsigned</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="mail"><a aria-hidden="true" tabindex="-1" href="#mail"><span class="icon icon-link"></span></a>Mail</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="mail-1"><a aria-hidden="true" tabindex="-1" href="#mail-1"><span class="icon icon-link"></span></a>Mail</h3><div class="wrap-body">
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token scope">Mail<span class="token punctuation">::</span></span><span class="token function">send</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'email.view'</span><span class="token punctuation">,</span> <span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token variable">$message</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Mail<span class="token punctuation">::</span></span><span class="token function">send</span><span class="token punctuation">(</span><span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'html.view'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'text.view'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token variable">$callback</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Mail<span class="token punctuation">::</span></span><span class="token function">queue</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'email.view'</span><span class="token punctuation">,</span> <span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token variable">$message</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Mail<span class="token punctuation">::</span></span><span class="token function">queueOn</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'queue-name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'email.view'</span><span class="token punctuation">,</span> <span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token variable">$callback</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Mail<span class="token punctuation">::</span></span><span class="token function">later</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'email.view'</span><span class="token punctuation">,</span> <span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token variable">$message</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 将所有电子邮件写入日志而不是发送</span>
</span><span class="code-line"><span class="token scope">Mail<span class="token punctuation">::</span></span><span class="token function">pretend</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="messages"><a aria-hidden="true" tabindex="-1" href="#messages"><span class="icon icon-link"></span></a>Messages</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token comment">// 这些可以用在传递给 Mail::send() 或 Mail::queue() 的 $message 实例上</span>
</span><span class="code-line"><span class="token variable">$message</span><span class="token operator">-></span><span class="token function">from</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'email@example.com'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Mr. Example'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$message</span><span class="token operator">-></span><span class="token function">sender</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'email@example.com'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Mr. Example'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$message</span><span class="token operator">-></span><span class="token function">returnPath</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'email@example.com'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$message</span><span class="token operator">-></span><span class="token function">to</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'email@example.com'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Mr. Example'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$message</span><span class="token operator">-></span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'email@example.com'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Mr. Example'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$message</span><span class="token operator">-></span><span class="token function">bcc</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'email@example.com'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Mr. Example'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$message</span><span class="token operator">-></span><span class="token function">replyTo</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'email@example.com'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Mr. Example'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$message</span><span class="token operator">-></span><span class="token function">subject</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Welcome to the Jungle'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$message</span><span class="token operator">-></span><span class="token function">priority</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$message</span><span class="token operator">-></span><span class="token function">attach</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo\bar.txt'</span><span class="token punctuation">,</span> <span class="token variable">$options</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 这使用内存中的数据作为附件</span>
</span><span class="code-line"><span class="token variable">$message</span><span class="token operator">-></span><span class="token function">attachData</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'bar'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Data Name'</span><span class="token punctuation">,</span> <span class="token variable">$options</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 在消息中嵌入文件并获取 CID</span>
</span><span class="code-line"><span class="token variable">$message</span><span class="token operator">-></span><span class="token function">embed</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo\bar.txt'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$message</span><span class="token operator">-></span><span class="token function">embedData</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Data Name'</span><span class="token punctuation">,</span> <span class="token variable">$options</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 获取底层的 Swift Message 实例</span>
</span><span class="code-line"><span class="token variable">$message</span><span class="token operator">-></span><span class="token function">getSwiftMessage</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="auth"><a aria-hidden="true" tabindex="-1" href="#auth"><span class="icon icon-link"></span></a>Auth</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="authentication"><a aria-hidden="true" tabindex="-1" href="#authentication"><span class="icon icon-link"></span></a>Authentication</h3><div class="wrap-body">
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token comment">// 判断当前用户是否通过认证</span>
</span><span class="code-line"><span class="token scope">Auth<span class="token punctuation">::</span></span><span class="token function">check</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 获取当前认证的用户</span>
</span><span class="code-line"><span class="token scope">Auth<span class="token punctuation">::</span></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 获取当前认证用户的ID</span>
</span><span class="code-line"><span class="token scope">Auth<span class="token punctuation">::</span></span><span class="token function">id</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 尝试使用给定的凭据对用户进行身份验证</span>
</span><span class="code-line"><span class="token scope">Auth<span class="token punctuation">::</span></span><span class="token function">attempt</span><span class="token punctuation">(</span><span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token variable">$email</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'password'</span> <span class="token operator">=></span> <span class="token variable">$password</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 通过将 true 传递给 Auth::attempt() 来“记住我”</span>
</span><span class="code-line"><span class="token scope">Auth<span class="token punctuation">::</span></span><span class="token function">attempt</span><span class="token punctuation">(</span><span class="token variable">$credentials</span><span class="token punctuation">,</span> <span class="token constant boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 登录一个请求</span>
</span><span class="code-line"><span class="token scope">Auth<span class="token punctuation">::</span></span><span class="token function">once</span><span class="token punctuation">(</span><span class="token variable">$credentials</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 将用户登录到应用程序</span>
</span><span class="code-line"><span class="token scope">Auth<span class="token punctuation">::</span></span><span class="token function">login</span><span class="token punctuation">(</span><span class="token scope">User<span class="token punctuation">::</span></span><span class="token function">find</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 将给定的用户 ID 登录到应用程序中</span>
</span><span class="code-line"><span class="token scope">Auth<span class="token punctuation">::</span></span><span class="token function">loginUsingId</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 从应用程序中注销用户</span>
</span><span class="code-line"><span class="token scope">Auth<span class="token punctuation">::</span></span><span class="token function">logout</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 验证用户的凭据</span>
</span><span class="code-line"><span class="token scope">Auth<span class="token punctuation">::</span></span><span class="token function">validate</span><span class="token punctuation">(</span><span class="token variable">$credentials</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 尝试使用 HTTP Basic Auth 进行身份验证</span>
</span><span class="code-line"><span class="token scope">Auth<span class="token punctuation">::</span></span><span class="token function">basic</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'username'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 执行无状态 HTTP 基本登录尝试</span>
</span><span class="code-line"><span class="token scope">Auth<span class="token punctuation">::</span></span><span class="token function">onceBasic</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 向用户发送密码提醒</span>
</span><span class="code-line"><span class="token scope">Password<span class="token punctuation">::</span></span><span class="token function">remind</span><span class="token punctuation">(</span><span class="token variable">$credentials</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token variable">$message</span><span class="token punctuation">,</span> <span class="token variable">$user</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="authorization"><a aria-hidden="true" tabindex="-1" href="#authorization"><span class="icon icon-link"></span></a>Authorization</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token comment">// 定义能力</span>
</span><span class="code-line"><span class="token scope">Gate<span class="token punctuation">::</span></span><span class="token function">define</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'update-post'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Class@method'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Gate<span class="token punctuation">::</span></span><span class="token function">define</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'update-post'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">,</span> <span class="token variable">$post</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token operator">...</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 传递多个参数</span>
</span><span class="code-line"><span class="token scope">Gate<span class="token punctuation">::</span></span><span class="token function">define</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'delete-comment'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">,</span> <span class="token variable">$post</span><span class="token punctuation">,</span> <span class="token variable">$comment</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// 检查能力</span>
</span><span class="code-line"><span class="token scope">Gate<span class="token punctuation">::</span></span><span class="token function">denies</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'update-post'</span><span class="token punctuation">,</span> <span class="token variable">$post</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Gate<span class="token punctuation">::</span></span><span class="token function">allows</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'update-post'</span><span class="token punctuation">,</span> <span class="token variable">$post</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Gate<span class="token punctuation">::</span></span><span class="token function">check</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'update-post'</span><span class="token punctuation">,</span> <span class="token variable">$post</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 指定一个用户进行检查</span>
</span><span class="code-line"><span class="token scope">Gate<span class="token punctuation">::</span></span><span class="token function">forUser</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">allows</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'update-post'</span><span class="token punctuation">,</span> <span class="token variable">$post</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 通过 User 模型，使用 Authorizable trait</span>
</span><span class="code-line"><span class="token scope">User<span class="token punctuation">::</span></span><span class="token function">find</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">can</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'update-post'</span><span class="token punctuation">,</span> <span class="token variable">$post</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">User<span class="token punctuation">::</span></span><span class="token function">find</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">cannot</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'update-post'</span><span class="token punctuation">,</span> <span class="token variable">$post</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// 拦截授权检查</span>
</span><span class="code-line"><span class="token scope">Gate<span class="token punctuation">::</span></span><span class="token function">before</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">,</span> <span class="token variable">$ability</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token scope">Gate<span class="token punctuation">::</span></span><span class="token function">after</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">,</span> <span class="token variable">$ability</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// 在 Blade 模板中检查</span>
</span><span class="code-line">@<span class="token function">can</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'update-post'</span><span class="token punctuation">,</span> <span class="token variable">$post</span><span class="token punctuation">)</span>
</span><span class="code-line">@endcan
</span><span class="code-line"><span class="token comment">// with else</span>
</span><span class="code-line">@<span class="token function">can</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'update-post'</span><span class="token punctuation">,</span> <span class="token variable">$post</span><span class="token punctuation">)</span>
</span><span class="code-line">@<span class="token keyword">else</span>
</span><span class="code-line">@endcan
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// 生成策略</span>
</span><span class="code-line">php artisan make<span class="token punctuation">:</span>policy PostPolicy
</span><span class="code-line"><span class="token comment">// `policy` 辅助函数</span>
</span><span class="code-line"><span class="token function">policy</span><span class="token punctuation">(</span><span class="token variable">$post</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">update</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">,</span> <span class="token variable">$post</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// 控制器授权</span>
</span><span class="code-line"><span class="token this keyword">$this</span><span class="token operator">-></span><span class="token function">authorize</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'update'</span><span class="token punctuation">,</span> <span class="token variable">$post</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// for $user</span>
</span><span class="code-line"><span class="token this keyword">$this</span><span class="token operator">-></span><span class="token function">authorizeForUser</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'update'</span><span class="token punctuation">,</span> <span class="token variable">$post</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="helper"><a aria-hidden="true" tabindex="-1" href="#helper"><span class="icon icon-link"></span></a>Helper</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="arrays"><a aria-hidden="true" tabindex="-1" href="#arrays"><span class="icon icon-link"></span></a>Arrays</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token comment">// 将给定的键/值对添加到数组中，如果</span>
</span><span class="code-line"><span class="token comment">// 数组中不存在给定的键</span>
</span><span class="code-line"><span class="token function">array_add</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 将数组的数组折叠成一个数组</span>
</span><span class="code-line"><span class="token function">array_collapse</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 将一个数组分成两个数组。一个有键，另一个有值</span>
</span><span class="code-line"><span class="token function">array_divide</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 用点展平多维关联数组</span>
</span><span class="code-line"><span class="token function">array_dot</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 获取所有给定的数组，除了指定的项目数组</span>
</span><span class="code-line"><span class="token function">array_except</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 返回数组中通过给定真值测试的第一个元素</span>
</span><span class="code-line"><span class="token function">array_first</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token variable">$key</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token variable">$default</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 从数组中剥离键</span>
</span><span class="code-line"><span class="token function">array_flatten</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 使用“点”表示法从给定数组中删除一个或多个数组项</span>
</span><span class="code-line"><span class="token function">array_forget</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'foo'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 点符号</span>
</span><span class="code-line"><span class="token function">array_forget</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'foo.bar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 使用“点”表示法从数组中获取项目</span>
</span><span class="code-line"><span class="token function">array_get</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'foo'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'default'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token function">array_get</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'foo.bar'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'default'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 使用“点”表示法检查给定项是否存在于数组中</span>
</span><span class="code-line"><span class="token function">array_has</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'products.desk'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 从给定数组中获取项目的子集</span>
</span><span class="code-line"><span class="token function">array_only</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 返回键数组 => 值</span>
</span><span class="code-line"><span class="token function">array_pluck</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 从数组中返回并删除“key”</span>
</span><span class="code-line"><span class="token function">array_pull</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 使用“点”表示法将数组项设置为给定值</span>
</span><span class="code-line"><span class="token function">array_set</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 点符号</span>
</span><span class="code-line"><span class="token function">array_set</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'key.subkey'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 根据给定闭包的结果对数组进行排序</span>
</span><span class="code-line"><span class="token function">array_sort</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 使用 sort 函数对数组进行递归排序</span>
</span><span class="code-line"><span class="token function">array_sort_recursive</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 使用给定的闭包过滤数组</span>
</span><span class="code-line"><span class="token function">array_where</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 数组的第一个元素</span>
</span><span class="code-line"><span class="token function">head</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 数组的最后一个元素</span>
</span><span class="code-line"><span class="token function">last</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="paths"><a aria-hidden="true" tabindex="-1" href="#paths"><span class="icon icon-link"></span></a>Paths</h3><div class="wrap-body">
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token comment">// 应用程序目录的完全限定路径</span>
</span><span class="code-line"><span class="token function">app_path</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 获取公共文件夹的路径</span>
</span><span class="code-line"><span class="token function">base_path</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 应用程序配置目录的完全限定路径</span>
</span><span class="code-line"><span class="token function">config_path</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 应用程序数据库目录的完全限定路径</span>
</span><span class="code-line"><span class="token function">database_path</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 获取版本控制的 Elixir 文件的路径：</span>
</span><span class="code-line"><span class="token function">elixir</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 公共目录的完全限定路径</span>
</span><span class="code-line"><span class="token function">public_path</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 获取存储文件夹的路径</span>
</span><span class="code-line"><span class="token function">storage_path</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="miscellaneous"><a aria-hidden="true" tabindex="-1" href="#miscellaneous"><span class="icon icon-link"></span></a>Miscellaneous</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token comment">// 验证器实例（Auth）</span>
</span><span class="code-line"><span class="token function">auth</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 生成对用户先前位置的重定向响应</span>
</span><span class="code-line"><span class="token function">back</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 使用 Bcrypt (Hash) 散列给定值</span>
</span><span class="code-line"><span class="token function">bcrypt</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'my-secret-password'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 从提供的项目创建一个集合实例</span>
</span><span class="code-line"><span class="token function">collect</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'taylor'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'abigail'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 获取配置变量的值</span>
</span><span class="code-line"><span class="token function">config</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app.timezone'</span><span class="token punctuation">,</span> <span class="token variable">$default</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 生成包含 CSRF 令牌值的 HTML 隐藏输入字段</span>
</span><span class="code-line"><span class="token punctuation">{</span><span class="token operator">!</span><span class="token operator">!</span> <span class="token function">csrf_field</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">!</span><span class="token operator">!</span><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment">// 检索当前 CSRF 令牌的值</span>
</span><span class="code-line"><span class="token variable">$token</span> <span class="token operator">=</span> <span class="token function">csrf_token</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 转储给定的变量并结束脚本的执行</span>
</span><span class="code-line"><span class="token function">dd</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 获取环境变量的值或返回默认值</span>
</span><span class="code-line"><span class="token variable">$env</span> <span class="token operator">=</span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'APP_ENV'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$env</span> <span class="token operator">=</span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'APP_ENV'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'production'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 将给定事件分派给它的侦听器：</span>
</span><span class="code-line"><span class="token function">event</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">UserRegistered</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 为给定类创建模型工厂构建器</span>
</span><span class="code-line"><span class="token variable">$user</span> <span class="token operator">=</span> <span class="token function">factory</span><span class="token punctuation">(</span><span class="token scope">App<span class="token punctuation">\</span>User<span class="token punctuation">::</span></span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">make</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 生成一个 HTML 隐藏输入字段，其中包含表单的 HTTP 谓词的欺骗值</span>
</span><span class="code-line"><span class="token punctuation">{</span><span class="token operator">!</span><span class="token operator">!</span> <span class="token function">method_field</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'delete'</span><span class="token punctuation">)</span> <span class="token operator">!</span><span class="token operator">!</span><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment">// 检索闪存到会话中的旧输入值</span>
</span><span class="code-line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">old</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'value'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">old</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'value'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'default'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 返回重定向器的实例以进行重定向：</span>
</span><span class="code-line"><span class="token keyword">return</span> <span class="token function">redirect</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/home'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 返回当前请求实例或获取输入项</span>
</span><span class="code-line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">request</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span> <span class="token variable">$default</span> <span class="token operator">=</span> <span class="token constant">null</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// 创建响应实例或获取响应工厂的实例</span>
</span><span class="code-line"><span class="token keyword">return</span> <span class="token function">response</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello World'</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token variable">$headers</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 用于获取/设置会话值</span>
</span><span class="code-line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">session</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">session</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token function">session</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 将简单地返回给定的值。</span>
</span><span class="code-line"><span class="token function">value</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token string single-quoted-string">'bar'</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 检索视图实例</span>
</span><span class="code-line"><span class="token keyword">return</span> <span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'auth.login'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 返回给定的值</span>
</span><span class="code-line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">with</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Foo</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">work</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="strings"><a aria-hidden="true" tabindex="-1" href="#strings"><span class="icon icon-link"></span></a>Strings</h3><div class="wrap-body">
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token comment">// 将值转换为驼峰大小写</span>
</span><span class="code-line"><span class="token function">camel_case</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 获取给定对象/类的类“basename”</span>
</span><span class="code-line"><span class="token function">class_basename</span><span class="token punctuation">(</span><span class="token variable">$class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 转义字符串</span>
</span><span class="code-line"><span class="token function">e</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'&#x3C;html>'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 确定给定字符串是否以给定子字符串开头</span>
</span><span class="code-line"><span class="token function">starts_with</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Foo bar.'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Foo'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 确定给定字符串是否以给定子字符串结尾</span>
</span><span class="code-line"><span class="token function">ends_with</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Foo bar.'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'bar.'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 将字符串转换为蛇形大小写</span>
</span><span class="code-line"><span class="token function">snake_case</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'fooBar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 限制字符串中的字符数</span>
</span><span class="code-line"><span class="token function">str_limit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 确定给定的字符串是否包含给定的子字符串</span>
</span><span class="code-line"><span class="token function">str_contains</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello foo bar.'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'foo'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 结果：foo/bar/</span>
</span><span class="code-line"><span class="token function">str_finish</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo/bar'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token function">str_is</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo*'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'foobar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token function">str_plural</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'car'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token function">str_random</span><span class="token punctuation">(</span><span class="token number">25</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token function">str_singular</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'cars'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token function">str_slug</span><span class="token punctuation">(</span><span class="token string double-quoted-string">"Laravel 5 Framework"</span><span class="token punctuation">,</span> <span class="token string double-quoted-string">"-"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 结果：FooBar</span>
</span><span class="code-line"><span class="token function">studly_case</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo_bar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token function">trans</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo.bar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token function">trans_choice</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo.bar'</span><span class="token punctuation">,</span> <span class="token variable">$count</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="urls-and-links"><a aria-hidden="true" tabindex="-1" href="#urls-and-links"><span class="icon icon-link"></span></a>URLs and Links</h3><div class="wrap-body">
<pre class="language-php"><code class="language-php code-highlight"><span class="code-line"><span class="token function">action</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'FooController@method'</span><span class="token punctuation">,</span> <span class="token variable">$parameters</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// HTML Link</span>
</span><span class="code-line"><span class="token function">asset</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'img/photo.jpg'</span><span class="token punctuation">,</span> <span class="token variable">$title</span><span class="token punctuation">,</span> <span class="token variable">$attributes</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// HTTPS link</span>
</span><span class="code-line"><span class="token function">secure_asset</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'img/photo.jpg'</span><span class="token punctuation">,</span> <span class="token variable">$title</span><span class="token punctuation">,</span> <span class="token variable">$attributes</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token function">route</span><span class="token punctuation">(</span><span class="token variable">$route</span><span class="token punctuation">,</span> <span class="token variable">$parameters</span><span class="token punctuation">,</span> <span class="token variable">$absolute</span> <span class="token operator">=</span> <span class="token constant boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token function">url</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'path'</span><span class="token punctuation">,</span> <span class="token variable">$parameters</span> <span class="token operator">=</span> <span class="token keyword">array</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token variable">$secure</span> <span class="token operator">=</span> <span class="token constant">null</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-not-exist"><div class="wrap-header h2wrap"><h2 id="另见"><a aria-hidden="true" tabindex="-1" href="#另见"><span class="icon icon-link"></span></a>另见</h2><div class="wrap-body">
<ul>
<li><a href="https://laravel.com/">Laravel 官网地址</a></li>
<li><a href="https://learninglaravel.net/cheatsheet/#">Laravel 8 Cheat Sheet</a></li>
</ul>
</div></div><div class="h2wrap-body"></div></div></div></div><footer class="footer-wrap"><footer class="max-container">© 2022 Kenny Wang.</footer></footer><script src="../data.js" defer></script><script src="../js/fuse.min.js" defer></script><script src="../js/main.js" defer></script><div id="mysearch"><div class="mysearch-box"><div class="mysearch-input"><div><svg xmlns="http://www.w3.org/2000/svg" height="1em" width="1em" viewBox="0 0 18 18">
  <path fill="currentColor" d="M17.71,16.29 L14.31,12.9 C15.4069846,11.5024547 16.0022094,9.77665502 16,8 C16,3.581722 12.418278,0 8,0 C3.581722,0 0,3.581722 0,8 C0,12.418278 3.581722,16 8,16 C9.77665502,16.0022094 11.5024547,15.4069846 12.9,14.31 L16.29,17.71 C16.4777666,17.8993127 16.7333625,18.0057983 17,18.0057983 C17.2666375,18.0057983 17.5222334,17.8993127 17.71,17.71 C17.8993127,17.5222334 18.0057983,17.2666375 18.0057983,17 C18.0057983,16.7333625 17.8993127,16.4777666 17.71,16.29 Z M2,8 C2,4.6862915 4.6862915,2 8,2 C11.3137085,2 14,4.6862915 14,8 C14,11.3137085 11.3137085,14 8,14 C4.6862915,14 2,11.3137085 2,8 Z"></path>
</svg><input id="mysearch-input" type="search" placeholder="搜索" autocomplete="off"><div class="mysearch-clear"></div></div><button id="mysearch-close" type="button">搜索</button></div><div class="mysearch-result"><div id="mysearch-menu"></div><div id="mysearch-content"></div></div></div></div></body>
</html>
